我有三张桌子:
Sentoni (id, person_id, duty_id, dayofduty, dayoff)
Ranks(id, rank)
Personel (id, rank, fullname)
在所有表中,id是主键。 Sentoni.person_id对应personel.id和ranking.id对应于resonel.rank。 我想获得Sentoni外部联合的personel.id,rank.rank和personel.fullname,其中dayofduty不是SPESIFIC日,比如2015-04-04。
我对这些表的加入感到非常困惑。 任何帮助表示赞赏。
答案 0 :(得分:1)
我试试这个
SELECT p.id, r.rank, p.fullname
FROM Sentoni s
INNER JOIN Personel p ON p.id = s.person_id
INNER JOIN Ranks r ON r.id = p.rank
WHERE s.dayofduty <> ...;
如果每个表中都有相应的记录,则应选择所需的信息。
答案 1 :(得分:1)
看看你的问题,我认为你正试图在MySQL的三个表上进行Full Outer Join。
MySQL中没有FULL OUTER JOIN
。请参阅7.2.12. Outer Join Simplification和12.2.8.1. JOIN Syntax:
您可以使用模仿
FULL OUTER JOIN
UNION(来自MySQL 4.0.0):有两个表t1,t2:
SELECT * FROM t1 LEFT JOIN t2 ON t1.id = t2.id UNION SELECT * FROM t1 RIGHT JOIN t2 ON t1.id = t2.id
有三个表t1,t2,t3:
SELECT * FROM t1 LEFT JOIN t2 ON t1.id = t2.id LEFT JOIN t3 ON t2.id = t3.id UNION SELECT * FROM t1 RIGHT JOIN t2 ON t1.id = t2.id LEFT JOIN t3 ON t2.id = t3.id UNION SELECT * FROM t1 RIGHT JOIN t2 ON t1.id = t2.id RIGHT JOIN t3 ON t2.id = t3.id
如果您只想获取常用记录;你可以使用内部加入:
SELECT p.id, r.rank, p.fullname
FROM Sentoni s
JOIN Personel p ON p.id = s.person_id
JOIN Ranks r ON r.id = p.rank
WHERE s.dayofduty <> 'Your_date';