SQL完全加入语法问题

时间:2017-06-17 13:58:44

标签: php mysql sql

我正在尝试在SQL中进行完全连接,但我一直收到此错误:

You have an error in your SQL syntax; check the ma…yroll P ON A.project_id = P.project_id' at line 1

这是查询。我无法弄清楚出了什么问题!

SELECT A.quantity, A.unit_price, P.paid_amount,  FROM assets A FULL JOIN payroll P ON A.project_id = P.project_id

谢谢!

1 个答案:

答案 0 :(得分:1)

FULL JOIN几乎不需要。为什么不这样做?

SELECT A.quantity, A.unit_price, P.paid_amount
FROM assets A LEFT JOIN
     payroll P
     ON A.project_id = P.project_id;

或者,如果每个表中可能有不同的项目:

SELECT a.quantity, a.unit_price, pr.paid_amount
FROM projects p LEFT JOIN
     assets a
     ON a.project_id = p.project_id LEFT JOIN
     payroll pr
     ON a.project_id = p.project_id
WHERE a.project_id IS NOT NULL or pr.project_id IS NOT NULL;

(大概如果你有project_id,那么你有一个projects表,每个项目有一行。)

即使在支持FULL JOIN的数据库中,这两个LEFT JOIN的效果也可能比FULL JOIN更好。