我有 3 张桌子,
- 桌子
senitems
- 桌子
inbox
- 桌子
outbox
如何加入 1 个表,请查看此链接以获取图像说明:
请帮忙
您不需要JOIN
为此,您需要的是UNION ALL
:
SELECT
SendingDateTime AS DateTime,
DestinationNumber AS PhoneNumber,
SMS,
'sentitems' AS Folder
FROM sentitems
UNION ALL
SELECT
ReceivingDateTime, SenderNumber, SMS,
'inbox'
FROM inbox
UNION ALL
SELECT
InserDateTime, DestinationNumber, SMS,
'outbox'
FROM outbox;
请注意:最终结果集的列名将从第一个查询中推断出来。
如果要对结果集进行排序,请将该查询放在子查询和ORDER BY
外部查询中,如下所示:
SELECT *
FROM
(
SELECT
SendingDateTime AS DateTime,
DestinationNumber AS PhoneNumber,
SMS,
'sentitems' AS Folder
FROM sentitems
UNION ALL
SELECT
ReceivingDateTime, SenderNumber, SMS,
'inbox'
FROM inbox
UNION ALL
SELECT
InserDateTime, DestinationNumber, SMS,
'outbox'
FROM outbox
) AS sub
ORDER BY DateTime