使用 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
我怎样才能做到这一点?
I have following query:
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
It works almost well, it returns:
[uniq] => some unique value
Is it possible to know which column this unique value was from?
I mean something like this:
[uniq] => some unique value
[from] => size
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的原始查询不需要子查询,也不需要
DISTINCT
。您可以使用:现在对于您的问题,如果四列没有重叠值,您可以使用:
Your original query does not need the subquery nor the
DISTINCT
. You could use:Now for your question, if the four columns have no overlapping values, you could use:
然后你可以使用 substring 删除右边的最后 2 个
then you can use substring to remove the last 2 from right