如何向该联合结果添加一列?
我有这个查询(为了简洁起见,我删除了一些键):
SELECT id as in_id, out_id, recipient, sender, read_flag
FROM received WHERE recipient=1
UNION ALL
SELECT in_id, id AS out_id, recipient, sender, read_flag
FROM sent WHERE sender=1
它结合了两个表的结果,显示给定用户发送和接收的消息。我想要做的是在结果中添加一个列/标志,以区分该行属于哪个表,这样当我显示它们时,我可以显示发送或接收消息的相关图标。我该如何添加这个?
I have this query (which I removed some keys from for brevity's sake):
SELECT id as in_id, out_id, recipient, sender, read_flag
FROM received WHERE recipient=1
UNION ALL
SELECT in_id, id AS out_id, recipient, sender, read_flag
FROM sent WHERE sender=1
Which combines the results from two tables showing messages sent and received by a given user. What I'd like to do is add a column/flag to the result to distinguish which table the row belongs to so when I display them I can show a relevant icon for sent or received messages. How would I add this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需向每个查询添加一个常量列即可。只要两个部分相同,类型是什么并不重要。因此您可以使用 0 和 1 或两个字符串,例如:
Just add a constant column to each query. It doesn't matter what the type is as long as it's the same in both parts. So you could use 0 and 1 or two strings, for example:
只需在每个选择中添加具有硬编码值的列即可:
Just add the column in each select with a hard coded value:
这将做到这一点:
This will do it: