MySQLi GROUP BY Column1(当Column1 = X时为Column2)

时间:2019-06-20 11:11:39

标签: mysql sql group-by

我想用1列做一个MySQL Group By,然后如果第1个像是某物,则按第2个分组。

我从来没有见过这样的东西,所以我在努力寻找方法。

$sql2 = "SELECT * FROM `UsageDailyUnusedSlots` 
WHERE Date = '$SQLDate' AND (RotaType LIKE '%Aural%' OR RotaType LIKE 
'%Colonoscopy%' OR RotaType LIKE '%Cystoscopy%' OR RotaType LIKE '%Derm%' OR 
RotaType LIKE 'ENT -%' OR RotaType LIKE '%Gastroscopy%' OR RotaType LIKE 
'%Gastroenterology%' OR RotaType LIKE '%Minor Surg%' OR(RotaType LIKE 
'%MRI%' AND (SlotType Like '%York or LTHT MRI%' OR SlotType LIKE '%ENT 
MRI%')) OR RotaType LIKE '%Ophthalmology%' OR RotaType LIKE '%Sigmoid%' OR 
RotaType LIKE '%Skinfinity%' OR RotaType LIKE '%Prostate%' OR RotaType LIKE 
'%Urology%' OR RotaType LIKE '%Vasectomy%')
GROUP BY RotaType, SlotType 
ORDER BY RotaType";

这是我的SQL查询,但是如果RotaType是LIKE胃镜检查,我只想按插槽类型分组

欢呼

1 个答案:

答案 0 :(得分:0)

  

(RotaType LIKE'%Aural%'或RotaType LIKE'%Clonstoscopy%'或RotaType LIKE'%Cystoscopy%'或RotaType LIKE'%Derm%'或RotaType LIKE'ENT-%'或RotaType LIKE'%胃镜% 'OR RotaType LIKE'%胃肠病%'OR RotaType LIKE'%Minor Surg%'OR(RotaType LIKE'%MRI%'AND(SlotType喜欢'%York或LTHT MRI%'或SlotType LIKE'%ENT MRI%'))或RotaType LIKE'%Ophthalmology%'或RotaType LIKE'%Sigmoid%'或RotaType LIKE'%Skinfinity%'或RotaType LIKE'%Prostate%'或RotaType LIKE'%Urology%'或RotaType LIKE'%Vasectomy%')< / p>

是组合的布尔条件

"SELECT * FROM `UsageDailyUnusedSlots` 
WHERE Date = '$SQLDate' AND (RotaType LIKE '%Aural%' OR RotaType LIKE
'%Colonoscopy%' OR RotaType LIKE '%Cystoscopy%' OR RotaType LIKE '%Derm%' OR 
RotaType LIKE 'ENT -%' OR RotaType LIKE '%Gastroscopy%' OR RotaType LIKE 
'%Gastroenterology%' OR RotaType LIKE '%Minor Surg%' OR(RotaType LIKE 
'%MRI%' AND (SlotType Like '%York or LTHT MRI%' OR SlotType LIKE '%ENT 
MRI%')) OR RotaType LIKE '%Ophthalmology%' OR RotaType LIKE '%Sigmoid%' OR 
RotaType LIKE '%Skinfinity%' OR RotaType LIKE '%Prostate%' OR RotaType LIKE 
'%Urology%' OR RotaType LIKE '%Vasectomy%')
GROUP BY CASE 
WHEN RotaType LIKE '%Gastroscopy%' THEN SlotType
ELSE RotaType END
ORDER BY RotaType";