问题是,我如何编写一个声明,使用sql join返回所有没有订单的客户?
表:tbl_member,tbl_order tbl_member由id,name组成, tbl_order由id,date,foodOrdered组成
答案 0 :(得分:1)
如果你离开了join,右边的表是nulkl的选择,它限制了那些不符合连接条件的行:
select t1.*
from tbl_member t1
left join tbl_member t2
on t1.id = t2.id -- assuming that t2.id relates to t1.id
and t2.date = current_date() -- today's date in mysql
where t2.id is null
答案 1 :(得分:0)
假设tbl_order日期是sql server的日期时间(可能应该是),你可以使用类似的东西:
declare @tbl_member table
(
id int,
fullname varchar(50)
)
declare @tbl_order table
(
id int,
orderdate datetime,
foodOrdered varchar(50)
)
INSERT INTO @tbl_member VALUES (1, 'George Washington')
INSERT INTO @tbl_member VALUES (2, 'Abraham Lincoln')
INSERT INTO @tbl_member VALUES (3, 'Mickey Mouse')
INSERT INTO @tbl_member VALUES (3, 'Donald Duck')
INSERT INTO @tbl_order VALUES (1, '2017-07-01 13:00:00', 'Fish and Chips')
INSERT INTO @tbl_order VALUES (2, '2017-07-03 08:00:00', 'Full English')
INSERT INTO @tbl_order VALUES (3, '2017-07-25 08:00:00', 'Veggie Burger')
INSERT INTO @tbl_order VALUES (3, '2017-07-25 12:00:00', 'Bangers and Mash')
SELECT id, fullname FROM @tbl_member WHERE id NOT IN
(SELECT id FROM @tbl_order
WHERE CAST(orderDate as date) = CAST(GETDATE() as Date))
如果您指定使用哪种flavor数据库,这会有所帮助,因为语法通常会略有不同。