七种JOINS SQL

数据库表

订单表 buy

支付表 pay

说明:id为7,8的支付记录,是其他表的订单记录。

查看pay表中关于buy表的未支付订单

SELECT
	b.* 
FROM
	buy b
	LEFT JOIN pay p ON b.id = p.buy_id 
WHERE
	p.id IS NULL;

查看pay表中关于buy表的已支付订单

SELECT
	b.* 
FROM
	buy b
	LEFT JOIN pay p ON b.id = p.buy_id 
WHERE
	p.id IS NOT NULL;

查看pau表中关于其他表的已支付订单

SELECT
	p.*
FROM
	buy b
	RIGHT JOIN pay p ON b.id = p.buy_id 
WHERE
	b.id IS  NULL;

posted @ 2022-06-10 15:36  羽绒333  阅读(30)  评论(0编辑  收藏  举报