不要在MySQL Query中强制执行其中一个条件。

时间:2011-07-30 16:09:02

标签: php mysql

我有3张桌子:

**people**
ID First Last Email        WageID  ShiftID
1 john Smith jhn@mail.com    2       3
2 sara doe   sar@mail.com    1         <----- no number



**wages**
ID Wage
1 300
2 500
3 800



**shifts**
ID Shift
1 day
2 night
3 half

我需要进行一次查询以获取所有表格中的信息 我有:

SELECT people.ID people.First people.Last people.Email wages.Wage, shifts.Shift 
FROM people, wages, shifts 
WHERE people.wageID = wages.ID
AND people.shiftID = shifts.ID

问题在于,因为“Sara Doe”没有指定的转移ID,所以它没有显示她,我认为因为最后的选择查询是否有办法让工作查询显示所有人而不改变表格?

如果我尝试OR people.shiftID = shifts.ID它仍然没有显示她

2 个答案:

答案 0 :(得分:3)

您应该使用OUTER JOIN来实现此目标

SELECT people.ID, people.First, people.Last, people.Email, wages.Wage, 
shifts.Shift FROM people
JOIN wages ON people.wageID = wages.ID
OUTER JOIN shifts ON people.shiftID = shifts.ID

答案 1 :(得分:0)

您应该使用一些左连接

select people.ID people.First people.Last people.Email wages.Wage, shifts.Shift
from people
    left join wages on wages.ID = people.WageID
    left join shifts on shifts.ID = people.ShiftID