我正在尝试像那样执行SQL查询
(SELECT ID, Name FROM Book)
UNION
(SELECT ID, Name FROM Author)
我希望在决赛桌中知道哪些ID来自书籍,哪些来自作者 设置一个标志或类似的东西。
答案 0 :(得分:3)
你可以这样做:
SELECT ID, Name, 'From Book' AS FromWhome
FROM Book
UNION
SELECT ID, Name, 'From Author'
FROM Author
答案 1 :(得分:2)
只需选择静态文字,例如
(SELECT ID, Name, "Book" FROM Book) UNION (SELECT ID, Name, "Author" FROM Author)
答案 2 :(得分:0)
选择值允许列和表达式,您也可以这样命名新列结果:
SELECT ID, Name, 'book' as tag FROM Book
UNION
SELECT ID, Name, 'author' as tag FROM Author