检索特定行中具有非空值的列的列名

时间:2017-04-10 14:50:31

标签: mysql

table showing entries in various columns

我想要在其URL标识的给定行中包含非空值的列的列名称,例如: - www.facebook.com。 查询的输出应该是 电子邮件,密码

1 个答案:

答案 0 :(得分:0)

你可以做这样的事情

select  url, group_concat(null_col separator ',')
from    (
            select  url, 'username' null_col from yourTable where username is null union all
            select  url, 'phone' from yourTable where phone is null union all
            select  url, 'email' from yourTable where email is null union all
            select  url, 'password' from yourTable where password is null union all
            select  url, 'login' from yourTable where login is null
        ) t1
group by url

请注意,在同一列中存储多个值是一个着名的坏主意,因为它会让一场噩梦加入,过滤或在该列上执行任何其他操作。

如果连接是您需要的最终结果,则上述查询应该可以解决问题。