如何用一条SQL语句查询3个表中的内容?

2025-04-06 04:00:16
推荐回答(2个)
回答1:

前提是表的结构应该是一样的或者处理成一样的~~~~

表A, aid, aname, atext
表B, bid, bname, btext
表C, cid, cname, ctext

SELECT * FROM (
SELECT aid AS id, aname AS name, atext AS [text] FROM A
UNION
SELECT bid AS id, bname AS name, btext AS [text] FROM B
UNION
SELECT cid AS id, cname AS name, ctext AS [text] FROM C
) Tmp WHERE name LIKE '%key%' OR [text] LIKE '%key%' ORDER BY id DESC

回答2:

select * from 表1,表2,表3