在结果中附加mysql数据

时间:2014-05-21 05:19:46

标签: php mysql

我有table member(id, name , email, altEmail, phone, altPhone, date) id 是唯一的 1,2,3,4,5,6,7 ..... 520,521

    id   email  altemail 

    1    2@h.v   2@T.h

    2    3@gy.c    -

    3       -     4@t.c

我需要输出为:

id   mail 

 1    2@h.v  

 1    2@T.h

 2    3@gy.c

 3    4@t.c

2 个答案:

答案 0 :(得分:2)

您可以使用UNION ALL

SELECT `id`, `email` mail FROM t WHERE  `email` <> '-'
UNION ALL 
SELECT `id`, `altemail` mail FROM t WHERE  `altemail` <> '-'
ORDER BY `id`

Demo

编辑空值

SELECT `id`, `email` mail FROM t WHERE  `email` <> ''  AND `email` IS NOT NULL
UNION ALL 
SELECT `id`, `altemail` mail FROM t WHERE  `altemail` <> '' AND `altemail` IS NOT NULL
ORDER BY `id`

Demo

答案 1 :(得分:0)

试试这个......

select id, email as mail from member union select id, altemail from member  order by id;