我有下表daysTurns
:
我怎样才能得到这样的结果?
date turn mail
2017-04-03 turn11 luis@geen.com
2017-04-03 turn12 luis@geen.com
2017-04-03 turn13 luis@geen.com
2017-04-03 turn21 luis@geen.com
2017-04-03 turn21 luis@geen.com
2017-04-03 turn21 luis@geen.com
2017-04-04 turn11 luis@geen.com
2017-04-04 turn12 luis@geen.com
2017-04-04 turn13 luis@geen.com
2017-04-04 turn21 luis@geen.com
2017-04-04 turn22 luis@geen.com
2017-04-04 turn23 luis@geen.com
我认为我需要像SELECT * FROM daysTurns
这样的查询。
这是预期的输出:
答案 0 :(得分:1)
如果您只有6列看起来像'turn__',那么请尝试这样做:
SELECT date, 'turn11' as turn, mail FROM daysTurns
WHERE turn11 is not null and turn11 <> ''
UNION ALL
SELECT date, 'turn12' as turn, mail FROM daysTurns
WHERE turn12 is not null and turn12 <> ''
UNION ALL
SELECT date, 'turn13' as turn, mail FROM daysTurns
WHERE turn13 is not null and turn13 <> ''
UNION ALL
SELECT date, 'turn21' as turn, mail FROM daysTurns
WHERE turn21 is not null and turn21 <> ''
UNION ALL
SELECT date, 'turn22' as turn, mail FROM daysTurns
WHERE turn22 is not null and turn22 <> ''
UNION ALL
SELECT date, 'turn23' as turn, mail FROM daysTurns
WHERE turn23 is not null and turn23 <> ''
答案 1 :(得分:0)
(代表OP发布)。
解决方案查询:
SET @mail = 'the_mail_that_i_need';
SELECT date, 'turn11' as turn, turn11 as mail FROM daysTurns
WHERE turn11 = @mail
UNION ALL
SELECT date, 'turn12' as turn, turn12 as mail FROM daysTurns
WHERE turn12 = @mail
UNION ALL
SELECT date, 'turn13' as turn, turn13 as mail FROM daysTurns
WHERE turn13 = @mail
UNION ALL
SELECT date, 'turn21' as turn, turn21 as mail FROM daysTurns
WHERE turn21 = @mail
UNION ALL
SELECT date, 'turn22' as turn, turn22 as mail FROM daysTurns
WHERE turn22 = @mail
UNION ALL
SELECT date, 'turn23' as turn, turn23 as mail FROM daysTurns
WHERE turn23 = @mail
ORDER BY date ASC