如何根据多行的结果使用 MySQL "CASE WHEN" 设置默认值
How to set a default value with MySQL "CASE WHEN" depending the result of multiple rows
这是我的问题:
通过以下查询,我得到了这个结果:
SELECT e.employes_id AS user_id, IFNULL(e.employes_initiales, 'Divers') AS initials, s.lieu, s.debut, s.fin, IFNULL(s.couleur, '#000000') AS color , s.id AS p_id,
CASE when couleur = '#00FF00' then 1 else 0 END as callservice
FROM ap_employes e LEFT JOIN
(ap_planing_employes sl JOIN
ap_planing s
ON sl.id_planing = s.id AND s.effacee = 0 AND
DATE('2018-04-27') BETWEEN CAST(s.debut AS DATE) AND CAST(s.fin AS DATE))
ON employes_id = sl.id_employes
WHERE e.employes_effacee = 0
ORDER BY e.employes_id ASC, FIELD(s.couleur,'#00FF00') desc, s.debut ASC, s.lieu ASC
结果
user_id initials lieu debut fin color p_id callservice
------- -------- ---------------------------------------- ------------------- ------------------- ------- ------ -------------
1 DV Test 2018-04-27 07:30:00 2018-04-27 07:30:00 #000000 526 0
1 DV Another Test 2018-04-27 09:00:00 2018-04-27 09:00:00 #000000 504 0
1 DV Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
2 SA Call service 2018-04-27 00:00:00 2018-04-28 00:00:00 #00FF00 336 1
2 SA Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
3 SW Customer x 2018-04-27 00:00:00 2018-04-27 00:00:00 #000000 547 0
3 SW Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
3 SW End at 2pm 2018-04-27 14:00:00 2018-04-27 14:00:00 #FF0000 538 0
4 JE Test2 2018-04-27 10:00:00 2018-04-27 10:00:00 #000000 541 0
4 JE Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
5 FR Holydays 2018-04-11 00:00:00 2018-04-29 00:00:00 #FF0000 75 0
5 FR Holydays 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
8 IE Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
999 Divers (NULL) (NULL) (NULL) #000000 (NULL) 0
好的,到目前为止它是完美的
如您所见,在第 4 行中,最后一列 "callservice" 是 1,因为根据我的要求,此行的颜色是正确的
现在是我的问题。
我想当没有人有呼叫服务对应的颜色时,它会自动分配给用户id 3
如果任何用户的颜色对应调用服务,那么他将collone "callservice"设为1。否则,默认必须是用户id 3 ...
我了解 CASE WHEN 函数的工作原理,但我无法在多行上应用它...
怎么办?
如果我的问题不是很清楚,我可以尝试改进
这是我想要得到的结果的一个例子。可以看到,nobody有callservice对应的颜色,所以callservice分配给用户ID 3
user_id initials lieu debut fin color p_id callservice
------- -------- ---------------------------------------- ------------------- ------------------- ------- ------ -------------
1 DV Test 2018-04-27 07:30:00 2018-04-27 07:30:00 #000000 526 0
1 DV Another Test 2018-04-27 09:00:00 2018-04-27 09:00:00 #000000 504 0
1 DV Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
2 SA Customer y 2018-04-27 00:00:00 2018-04-28 00:00:00 #000000 336 0
2 SA Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
3 SW Customer x 2018-04-27 00:00:00 2018-04-27 00:00:00 #000000 547 1
3 SW Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 1
3 SW End at 2pm 2018-04-27 14:00:00 2018-04-27 14:00:00 #FF0000 538 1
4 JE Test2 2018-04-27 10:00:00 2018-04-27 10:00:00 #000000 541 0
4 JE Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
5 FR Holydays 2018-04-11 00:00:00 2018-04-29 00:00:00 #FF0000 75 0
5 FR Holydays 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
8 IE Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
999 Divers (NULL) (NULL) (NULL) #000000 (NULL) 0
好吧,这可能是一个复杂的解决方案,但是...
SET @couleur_number = 0;
DROP TEMPORARY TABLE IF EXISTS temp_table;
CREATE TEMPORARY TABLE temp_table ENGINE=MEMORY AS
SELECT e.employes_id AS user_id, IFNULL(e.employes_initiales, 'Divers') AS initials,
s.lieu, s.debut, s.fin, IFNULL(s.couleur, '#000000') AS color , s.id AS p_id,
CASE when couleur = '#00FF00' then @couleur_number := @couleur_number + 1
FROM ap_employes e LEFT JOIN
(ap_planing_employes sl JOIN
ap_planing s
ON sl.id_planing = s.id AND s.effacee = 0 AND
DATE('2018-04-27') BETWEEN CAST(s.debut AS DATE) AND CAST(s.fin AS DATE))
ON employes_id = sl.id_employes
WHERE e.employes_effacee = 0
ORDER BY e.employes_id ASC, FIELD(s.couleur,'#00FF00') desc, s.debut ASC, s.lieu ASC;
SELECT user_id, initials,
s.lieu, s.debut, s.fin, color, p_id
CASE when @couleur_number > 0
then
CASE when color='#00FF00'
then 1 else 0
end
else
CASE when user_id='3'
then 1 else 0
end
END as callservice
FROM temp_table;
这是我的问题:
通过以下查询,我得到了这个结果:
SELECT e.employes_id AS user_id, IFNULL(e.employes_initiales, 'Divers') AS initials, s.lieu, s.debut, s.fin, IFNULL(s.couleur, '#000000') AS color , s.id AS p_id,
CASE when couleur = '#00FF00' then 1 else 0 END as callservice
FROM ap_employes e LEFT JOIN
(ap_planing_employes sl JOIN
ap_planing s
ON sl.id_planing = s.id AND s.effacee = 0 AND
DATE('2018-04-27') BETWEEN CAST(s.debut AS DATE) AND CAST(s.fin AS DATE))
ON employes_id = sl.id_employes
WHERE e.employes_effacee = 0
ORDER BY e.employes_id ASC, FIELD(s.couleur,'#00FF00') desc, s.debut ASC, s.lieu ASC
结果
user_id initials lieu debut fin color p_id callservice
------- -------- ---------------------------------------- ------------------- ------------------- ------- ------ -------------
1 DV Test 2018-04-27 07:30:00 2018-04-27 07:30:00 #000000 526 0
1 DV Another Test 2018-04-27 09:00:00 2018-04-27 09:00:00 #000000 504 0
1 DV Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
2 SA Call service 2018-04-27 00:00:00 2018-04-28 00:00:00 #00FF00 336 1
2 SA Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
3 SW Customer x 2018-04-27 00:00:00 2018-04-27 00:00:00 #000000 547 0
3 SW Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
3 SW End at 2pm 2018-04-27 14:00:00 2018-04-27 14:00:00 #FF0000 538 0
4 JE Test2 2018-04-27 10:00:00 2018-04-27 10:00:00 #000000 541 0
4 JE Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
5 FR Holydays 2018-04-11 00:00:00 2018-04-29 00:00:00 #FF0000 75 0
5 FR Holydays 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
8 IE Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
999 Divers (NULL) (NULL) (NULL) #000000 (NULL) 0
好的,到目前为止它是完美的
如您所见,在第 4 行中,最后一列 "callservice" 是 1,因为根据我的要求,此行的颜色是正确的
现在是我的问题。 我想当没有人有呼叫服务对应的颜色时,它会自动分配给用户id 3
如果任何用户的颜色对应调用服务,那么他将collone "callservice"设为1。否则,默认必须是用户id 3 ...
我了解 CASE WHEN 函数的工作原理,但我无法在多行上应用它...
怎么办?
如果我的问题不是很清楚,我可以尝试改进
这是我想要得到的结果的一个例子。可以看到,nobody有callservice对应的颜色,所以callservice分配给用户ID 3
user_id initials lieu debut fin color p_id callservice
------- -------- ---------------------------------------- ------------------- ------------------- ------- ------ -------------
1 DV Test 2018-04-27 07:30:00 2018-04-27 07:30:00 #000000 526 0
1 DV Another Test 2018-04-27 09:00:00 2018-04-27 09:00:00 #000000 504 0
1 DV Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
2 SA Customer y 2018-04-27 00:00:00 2018-04-28 00:00:00 #000000 336 0
2 SA Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
3 SW Customer x 2018-04-27 00:00:00 2018-04-27 00:00:00 #000000 547 1
3 SW Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 1
3 SW End at 2pm 2018-04-27 14:00:00 2018-04-27 14:00:00 #FF0000 538 1
4 JE Test2 2018-04-27 10:00:00 2018-04-27 10:00:00 #000000 541 0
4 JE Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
5 FR Holydays 2018-04-11 00:00:00 2018-04-29 00:00:00 #FF0000 75 0
5 FR Holydays 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
8 IE Something 2018-04-27 12:00:00 2018-04-27 12:00:00 #0000FF 451 0
999 Divers (NULL) (NULL) (NULL) #000000 (NULL) 0
好吧,这可能是一个复杂的解决方案,但是...
SET @couleur_number = 0;
DROP TEMPORARY TABLE IF EXISTS temp_table;
CREATE TEMPORARY TABLE temp_table ENGINE=MEMORY AS
SELECT e.employes_id AS user_id, IFNULL(e.employes_initiales, 'Divers') AS initials,
s.lieu, s.debut, s.fin, IFNULL(s.couleur, '#000000') AS color , s.id AS p_id,
CASE when couleur = '#00FF00' then @couleur_number := @couleur_number + 1
FROM ap_employes e LEFT JOIN
(ap_planing_employes sl JOIN
ap_planing s
ON sl.id_planing = s.id AND s.effacee = 0 AND
DATE('2018-04-27') BETWEEN CAST(s.debut AS DATE) AND CAST(s.fin AS DATE))
ON employes_id = sl.id_employes
WHERE e.employes_effacee = 0
ORDER BY e.employes_id ASC, FIELD(s.couleur,'#00FF00') desc, s.debut ASC, s.lieu ASC;
SELECT user_id, initials,
s.lieu, s.debut, s.fin, color, p_id
CASE when @couleur_number > 0
then
CASE when color='#00FF00'
then 1 else 0
end
else
CASE when user_id='3'
then 1 else 0
end
END as callservice
FROM temp_table;