SQL:使用select更新表

时间:2015-10-04 05:54:51

标签: mysql sql

我有这样的查询

UPDATE t_prd_cost_compare 
SET 
    2015_AUG_PRD_UNIT_PRICE=i.PRD_UNIT_PRICE, 
    2015_AUG_PRD_SELLING_PRICE=i.PRD_SELLING_PRICE,
    2015_AUG_PRD_IN_PATIENT_LIST_PRICE=i.PRD_IN_PATIENT_LIST_PRICE,
    2015_AUG_PRD_OUT_PATIENT_LIST_PRICE=i.PRD_OUT_PATIENT_LIST_PRICE
FROM (
    SELECT PRODUCTID,PRD_UNIT_PRICE,PRD_SELLING_PRICE,PRD_IN_PATIENT_LIST_PRICE,PRD_OUT_PATIENT_LIST_PRICE
    FROM t_product_catalog
    LEFT JOIN T_adjust ON IAJ_PRODUCTID=PRODUCTID AND IAJ_ADJNO IS NULL
    WHERE PRODUCTID>1 AND (DATE(IAJ_DATE) = '2015-01-01')
    GROUP BY IAJ_PRODUCTID
    ) AS i
WHERE i.PRODUCTID = t_prd_cost_compare.PRODUCTID

我得到这样的错误

  

错误代码:1064
      您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便在'FROM('附近使用正确的语法       在第7行选择PRODUCTID,PRD_UNIT_PRICE,PRD_SELLING_PRICE,PRD_IN_PATIENT_LIST_PRI'

我已经检查过select语句是否正确,但我仍然收到错误!

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

问题解决了,这是解决方案

Update
  Competition as C
  inner join (
    select CompetitionId, count(*) as NumberOfTeams
    from PicksPoints as p
    where UserCompetitionID is not NULL
    group by CompetitionID
  ) as A on C.CompetitionID = A.CompetitionID
set C.NumberOfTeams = A.NumberOfTeams

参考:mysql update query with sub query