mysql sql 结果集 二选一的想法 5C

SELECT * from i_user_channel_set WHERE subject_type=1 SELECT * from i_user_channel_set WHERE subject_type=2 这两个语句如果想第一个没有查询到结果就查询第二个,第一个有结果就忽略第二个语句 单纯用sql可以做到嘛

6个回答

SELECT * from i_user_channel_set WHERE subject_type=1 or subject_type=1

SELECT * FROM i_user_channel_set WHERE subject_type IN (

SELECT IF subject_type IS NULL THEN (SELECT subject_type from i_user_channel_set WHERE subject_type=2 ) ELSE THEN subject_type END AS subject_type FROM i_user_channel_set WHERE subject_type=1

);

-- 真是闲着没事,吃饱了给自己找不自在

select * from i_user_channel_set where subject_type = if((select count(0) from i_user_channel_set where subject_type=1)>0, 1, 2);

首先我这个方案肯定是可以实现,其次脚本逻辑清晰,效率较高,大家可以执行实验一下

想了半天,应该是不行,试了很多函数也没法实现

( SELECT * from i_user_channel_set WHERE subject_type=1 ) UNION ALL
( SELECT * from i_user_channel_set WHERE subject_type=2 AND ( NOT EXISTS ( SELECT * from i_user_channel_set WHERE subject_type=1 ) ) )

这样就可以

SELECT * from i_user_channel_set WHERE subject_type=1

UNION

SELECT * from i_user_channel_set WHERE subject_type=2 and (SELECT count(1) from i_user_channel_set WHERE subject_type=1 )=0

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问