假设有一个Order表和一个User表, 需要在一条语句里面查询所有没有订单的用户应该怎么写?
select * from user
where not exists (select 1 from trade where userid=user. id)
猜测你的表结构,仅供参考,当然用join和多表查询也是可以的
那个的看你做什么用,如果查询单条, select a.* from table a where not exists(select 1 from table b where a.id = b.id) limit 1这样应该在毫秒返回
如果查询所有就用
selecta.* from table a left join table b on a.id = b.id where b.id IS NULL这样会比较快,查询共同的直接inner join反正mysql不咋好用把笛卡尔积计算方式封装了,特麻烦,不过谁叫他免费
2 回答2.5k 阅读✓ 已解决
5 回答1.5k 阅读
2 回答1.1k 阅读✓ 已解决
2 回答2.2k 阅读
3 回答781 阅读✓ 已解决
2 回答556 阅读✓ 已解决
1 回答858 阅读✓ 已解决
SELECT U.ID AS UserID
FROM Users U
LEFT JOIN Order O ON O.UserID = U.ID
WHERE O.ID IS NULL