mysql查詢數據
?一個數據表里面有個字段type,改字段type可以有三個值1,2,3,當type的值有1或者2時,就只查詢type=2的數據,當type只有1的值時就只查詢等于1的數據
比如說這個數據,group_id等于10的數據type只有1,就要查詢type=1的這條,group_id等于12的這條數據type的值有1和2,就只查詢type=2的這個數據,也就是說,現在要查詢的數據就只有id為46和48的這兩個數據,請問一下這個sql語句怎么寫,條件就只能用type做為條件
?一個數據表里面有個字段type,改字段type可以有三個值1,2,3,當type的值有1或者2時,就只查詢type=2的數據,當type只有1的值時就只查詢等于1的數據
比如說這個數據,group_id等于10的數據type只有1,就要查詢type=1的這條,group_id等于12的這條數據type的值有1和2,就只查詢type=2的這個數據,也就是說,現在要查詢的數據就只有id為46和48的這兩個數據,請問一下這個sql語句怎么寫,條件就只能用type做為條件
2021-08-09
舉報
2021-11-12
select *
from 表名
where type=2
union
select t1.*
from (
??? select *
??? from 表名
??? where type=1
??? ) t1
left join
(
??? select *
??? from 表名
??? where type=2
) t2
on t1.group_id=t2.group_id
where t2.type is null
2021-08-11
select * from 表名 where type=2? or (type=1 and type not =2);