mysql每日一题0721 关联查询

该博客提供了两个SQL查询解决方案,用于从T0721表中找出那些只有一种状态('FULL')的DOCNUM。解决方案1使用LEFT JOIN和COUNT DISTINCT,而解决方案2则利用了GROUP BY和GROUP_CONCAT函数来实现相同的目标。
摘要由CSDN通过智能技术生成

😄题目如下

image
测试数据
CREATE TABLE T0721 
(
DOCNUM INT,
STATUS VARCHAR(26)
);
INSERT INTO  T0721 VALUES (33,'FULL');
INSERT INTO  T0721 VALUES (33,'NOFULL');
INSERT INTO  T0721 VALUES (34,'FULL');
INSERT INTO  T0721 VALUES (35,'FULL');
INSERT INTO  T0721 VALUES (35,'NOFULL');
INSERT INTO  T0721 VALUES (36,'FULL');
INSERT INTO  T0721 VALUES (37,'FULL');
INSERT INTO  T0721 VALUES (38,'FULL');
INSERT INTO  T0721 VALUES (38,'NOFULL')
参考答案
  • solution1
SELECT
	a.docnum 
FROM
	t0721 a
	LEFT JOIN ( SELECT docnum, count( DISTINCT STATUS ) status_num FROM t0721 GROUP BY 1 ) b
	 ON a.DOCNUM = b.docnum 
WHERE
	status_num = 1 
	AND a.STATUS IN ('FULL')
  • solution2
select a.docnum
from t0721 a
group by 1
having group_concat(status)='FULL'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值