3

我正在尝试执行这样的 SQL 查询

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

我想在决赛桌中知道哪些 id 来自书本,哪些来自作者设置标志或类似的东西。

4

3 回答 3

3

你可以这样做:

SELECT ID, Name, 'From Book' AS FromWhome 
FROM Book
UNION
SELECT ID, Name, 'From Author'
FROM Author
于 2012-09-30T11:12:02.110 回答
2

只需选择一个静态文本,例如

(SELECT ID, Name, "Book" FROM Book) UNION (SELECT ID, Name, "Author" FROM Author)
于 2012-09-30T11:12:24.887 回答
0

选择值允许列和表达式,您也可以用这种方式命名新列结果:

SELECT ID, Name, 'book' as tag FROM Book
UNION 
SELECT ID, Name, 'author' as tag FROM Author
于 2012-09-30T11:12:43.587 回答