Update query resulting wrongly

拟墨画扇 提交于 2019-12-04 07:15:38

问题


I have table called company_emp. In that table I have 6 columns related to employees:

  1. empid
  2. ename
  3. dob
  4. doj, ...

I have another table called bday. In that I have only 2 columns; empid and dob.

I have this query:

select empid, dob 
from company_emp 
where dob like '01/05/2011'

It shows some list of employees.

In the same way I have queried with table bday it listed some employees.

Now I want to update the company_emp table for employees who have date '01/05/2011'.

I have tried a query like this:

update company_name a
 set dob = (select dob from bday b 
            where b.empid=a.empid 
              and to_char(a.dob,'dd/mm/yyyy') = '01/05/2011'}

Then all the records in that row becoming null. How can I fix this query?


回答1:


You're updating every row in the company_name/emp table.

You can fix that with a correlated subquery to make sure the row exists, or more efficiently by placing a primary or unique key on bday.empid and querying:

update (
  select c.dob to_dob,
         d.dob from_dob
  from   company_emp c join dob d on (c.empid = d.empid)
  where  d.dob = date '2011-05-01')
set to_dob = from_dob

Syntax not tested.



来源:https://stackoverflow.com/questions/16505612/update-query-resulting-wrongly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!