0

这是我的表格的相关列

  • bia_panels( id, sign_id, value, project_id)

  • bia_clients( id, name)

  • bia_projects( id, name, client_id, city_id)

  • bia_cities( id, name)

我正在尝试更新bia_panels.project_idbia_projects.idwhere bia_panels.value = bia_clients.name和 thepanels.project_id =000并且值不是空白当然我必须使用多个连接才能到达那里

    -- UPDATE
SELECT * FROM
`bia_panels` AS t1
    JOIN  bia_clients AS t2 
        ON t1.value = t2.name 
    JOIN bia_projects AS  t3 
        ON t2.id = t3.client_id 


-- SET t1.project_id = t3.id

-- WHERE t1.value<>'' AND t1.project_id = '000' 
WHERE t1.value <>''

问题是这没有给我正确的结果(我的项目 ID 在连接中的某处不正确,返回多个结果,因此它们中断

我知道一旦我能够正确选择部分,我就可以使用更新

例如,可能有多个面板,value=client.name但并非所有面板都是相同的项目 ID

4

2 回答 2

0

和 bia_panels.ID = bia_panels.project_id

您的选择查询中缺少连接条件,应该像这样添加

 JOIN bia_projects ON bia_clients.id = bia_projects.client_id  and bia_panels.ID = bia_panels.project_id

以下查询应该给出正确的输出

SELECT  sign_id, value, left(sign_id, 3) AS city_ID  ,  
bia_clients.id AS 'client id',  bia_projects.id AS proj_id , bia_cities.id
FROM bia_panels
 JOIN  bia_clients ON bia_panels.value = bia_clients.name 
 JOIN bia_projects ON bia_clients.id = bia_projects.client_id  and bia_panels.ID = bia_panels.project_id
JOIN bia_cities ON bia_projects.city_id = bia_cities.id WHERE bia_panels.value<>'' AND bia_panels.project_id = '000' ORDER BY value
于 2013-07-06T07:35:43.317 回答
0

我会将您的查询重新组织为 UPDATE 查询:

UPDATE bia_panels p, bia_clients c, bia_projects t
  SET p.project_id=t.id
  WHERE p.value=c.name
    AND t.client_id=c.id
    AND p.project_id=''
于 2013-09-27T14:20:17.367 回答