是否可以在sql查询期间设置标志

时间:2012-09-30 11:10:21

标签: sql select union

我正在尝试像那样执行SQL查询

(SELECT ID, Name FROM Book)
UNION
(SELECT ID, Name FROM Author)

我希望在决赛桌中知道哪些ID来自书籍,哪些来自作者 设置一个标志或类似的东西。

3 个答案:

答案 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