我有两个表,但编写查询时遇到了困难。 我试过LEFT JOIN
SELECT swimmer.id, pool,last_name, first_name, paid, membership.month, datePaid, membership.fee, membership.datePaid
FROM swimmer
LEFT JOIN membership
ON membership.swimmer_id = swimmer.id
WHERE membership.month = 'September'
OR membership.month IS NULL;
当我使用此代码时,我错过了那些为8月付款的人:
Pool FirstName LastName DatePaid Fee Method
B Sarah Delt null null null
B Pavle North null null null
S Daniel Key 2018-09-01 2000 Cash
在我的主页上,我有几个月的下拉列表。我希望显示当月支付的所有游泳运动员以及那些没有付费的游泳运动员,但他们的值应该为空(exept Pool,FirstName和SecondName)
游泳桌:
NameID PoolName FirstName LastName
1 B Anna Pier
2 B Wolf Sedveh
3 B Sarah Delt
4 B Pavle North
5 S Daniel Keys
会员资格表:
ID Year Month DatePaid Fee Method SwimmerId(FK)
1 2017 August 2017-08-01 1000 Cash 1
2 2017 August 2017-08-01 2000 Cash 2
3 2017 September 2017-09-01 2000 Cash 5
最后,我需要这样的表:
Pool FirstName LastName DatePaid Fee Method
B Anna Pier null null null
B Wolf Sedveh null null null
B Sarah Delt null null null
B Pavle North null null null
S Daniel Keys 2017-09-01 2000 Cash
所以,基本上,我需要一个查询,显示所选月份(和年份)的付费会员资格。对于付费的人 - 返回数据,对于那些没有 - 返回null的人。 我希望我解释得很好。
答案 0 :(得分:2)
SELECT swimmer.id, pool,last_name,
first_name, paid, membership.month,
datePaid, membership.fee, membership.datePaid
FROM swimmer
LEFT JOIN membership
ON membership.swimmer_id = swimmer.id
AND membership.month = 'September'
AND membership.year = '2017'
LEFT JOIN
:
LEFT
表,请添加WHERE
条件。RIGHT
条件的ON
表格。