php – 使用DISTINCT和UNION在SQL查询中获取字段名称

我有以下查询:

SELECT DISTINCT(uniq)
FROM
(
    SELECT sex AS uniq FROM type4
    UNION
    SELECT fason AS uniq FROM type4
    UNION
    SELECT color AS uniq FROM type4
    UNION
    SELECT size AS uniq FROM type4
) AS Temp

它运作得很好,它返回:

[uniq] => some unique value

是否可以知道此唯一值来自哪个列?
我的意思是这样的:

[uniq] => some unique value
[from] => size

我怎样才能做到这一点?

解决方法:

SELECT DISTINCT(uniq)
FROM
(
    SELECT (sex + ' 1') AS uniq FROM type4
    UNION
    SELECT (fason + ' 2') AS uniq FROM type4
    UNION
    SELECT (color + ' 3') AS uniq FROM type4
    UNION
    SELECT (size + ' 4') AS uniq FROM type4
) AS Temp

然后你可以使用substring从右边删除最后2

上一篇:WPF 3D 轮播可操控面板


下一篇:npm、yarn是什么?如何安装?