使用子查询更新导致错误

时间:2016-10-07 18:56:13

标签: sql postgresql subquery

以下查询有什么问题,我尝试使用每周一次更新每日表中的计数,我使用每周相同项目的计数来更新每日表中的每个项目的计数

  select  a.ik , a.date , d.count
  from Table1 a , Table2 b ,
  ( select count from Table2 b
   where b.ik = a.ik and wk in 
   ( select wk from calendar_table c,Table1  where c.calendar_date = Table1.date)) as d 

表1

    ik,           , date
    133;0;"002996";"2014-06-26"
     11;0;"003406";"2014-06-22"

表2

     ik, wk , count
     368;201605;0
     377;201438;0

calendar_table

   date, wk
   "2013-08-15";201329
    "2019-09-05";201932

2 个答案:

答案 0 :(得分:1)

select  a.ik , a.date , b.count
  from Table1 a
  join Table2 b on b.ik=a.ik
  join Calendar_table c on c.calendar_date=a.date

答案 1 :(得分:0)

如果你想更新table1,这样的事情应该这样做:

 if($fixedrange=="Today"){
    $today = date('Y-m-d');
    $fixeddate = 'OR  date(o.`custom_date`) = curdate() ' ;
}

$sql = " SELECT * FROM order_items AS oi
  INNER JOIN orders o ON o.id=oi.`order_id` 
  WHERE oi.`product_name` LIKE concat('%', $pname,'%') 
  $fixeddate
  AND o.status = '1' 
  ORDER BY o.custom_date DESC ; ";

根据您的示例数据,这不会更新任何内容,因为update table1 set "count" = t.cnt from ( select t2.ik, c.date, t2."count" as cnt from table2 t2 join calendar_table c on c.wk = t2.wk ) t where table1.ik = t.ik; 中的wktable2中的wk值不匹配