Sql 多次并集后选择
我需要从多重并集中选择不同的值。 我需要在不同数据库的多个表上使用union。 但我尝试使用以下代码:Sql 多次并集后选择,sql,union,Sql,Union,我需要从多重并集中选择不同的值。 我需要在不同数据库的多个表上使用union。 但我尝试使用以下代码: SELECT DISTINCT name ((SELECT name FROM tab1) UNION (SELECT name FROM tab2)) 我有一个错误: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the rig
SELECT DISTINCT name ((SELECT name FROM tab1) UNION (SELECT name FROM tab2))
我有一个错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION (SELECT name FROM tab2 )' at line 1
试试这个:
SELECT DISTINCT name FROM (
SELECT name FROM tab1
UNION
SELECT name FROM tab2
) TABLEALIAS
试试这个:
SELECT DISTINCT name FROM (
SELECT name FROM tab1
UNION
SELECT name FROM tab2
) TABLEALIAS
您不需要使用“distinct”。联合已经给了你明确的结果
SELECT name FROM tab1
UNION
SELECT name FROM tab2
这将为您提供正确的结果。顺便说一句,如果您想从不同的数据库获取数据,您需要使用db名称。请参考以下内容
SELECT name FROM db1.tab1
UNION
SELECT name FROM db2.tab2
您不需要使用“distinct”。联合已经给了你明确的结果
SELECT name FROM tab1
UNION
SELECT name FROM tab2
这将为您提供正确的结果。顺便说一句,如果您想从不同的数据库获取数据,您需要使用db名称。请参考以下内容
SELECT name FROM db1.tab1
UNION
SELECT name FROM db2.tab2
您需要一个来自的
,以及内部结果的别名:
SELECT DISTINCT name
FROM (
SELECT name FROM tab1
UNION
SELECT name FROM tab2
) x
但是,您根本不需要独立的
,联合
已经删除了重复项。您需要一个来自
的,以及内部结果的别名:
SELECT DISTINCT name
FROM (
SELECT name FROM tab1
UNION
SELECT name FROM tab2
) x
但是,您根本不需要DISTINCT
,因为UNION
已经删除了重复项