我想执行两个具有相同列数的查询。这是我的问题,请帮我看看结果:
SELECT A.*
FROM (SELECT *
FROM yh5lw_onlinecourse
WHERE state = 1
ORDER BY id DESC) A
union
SELECT A1.*
FROM (SELECT *
FROM yh5lw_practicetest
WHERE state = 1
ORDER BY id DESC) A1
答案 0 :(得分:1)
我想你想要这个:
SELECT * FROM
(SELECT A.id as id, A.field1 as outputname1, A.field2 as outputname2, ... and so on
FROM yh5lw_onlinecourse A
WHERE state = 1
UNION
SELECT A1.*
FROM yh5lw_practicetest A1
WHERE state = 1) myUnion
order by myUnion.id DESC
注意:
如果您使用UNION而不是UNION ALL,则会丢弃所有重复的值 -
第一个查询必须有别名,因为这样你就命名了yuor输出表。第二个查询(在UNION之后)可以使用*通配符(但字段数必须相同)