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