Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL:从两个不同的表中选择Distinct?_Mysql - Fatal编程技术网

MySQL:从两个不同的表中选择Distinct?

MySQL:从两个不同的表中选择Distinct?,mysql,Mysql,我有两个不同的表,每个表都有一个名为product\u type的列。如何在两个表中获得产品类型的不同值?只是想澄清一下,如果两个表都有“钻石”类型的产品,我只希望它返回一次。基本上就好像这两个表组合在一起,我从中选择了不同的产品类型 谢谢 请注意,UNION子句返回字段的唯一值,如果希望返回所有值,则必须使用UNION ALL select product_type from table_a union product_type from table_b 对包含联合的子查询使用distinc

我有两个不同的表,每个表都有一个名为product\u type的列。如何在两个表中获得产品类型的不同值?只是想澄清一下,如果两个表都有“钻石”类型的产品,我只希望它返回一次。基本上就好像这两个表组合在一起,我从中选择了不同的产品类型


谢谢

请注意,UNION子句返回字段的唯一值,如果希望返回所有值,则必须使用UNION ALL

select product_type from table_a
union
product_type from table_b

对包含联合的子查询使用distinct

select distinct product_type from (
    select product_type from table 1
    union 
    select product_type from table 2
) t

使用distinct和union:

select distinct product_type from table1
union
select distinct product_type from table2

联合会将在合并结果时删除重复项。

谢谢,有一个问题。。“t”应该在结尾吗?这是干什么用的?谢谢T是子查询的别名,因此,您可以说T.produc_type,它的意思是“来自称为T的子查询的字段产品_type”,您需要外部查询吗?我相信工会只会返回不同的意见分歧。(UNION ALL将需要外部查询)@JamesMoore是的,否则DISTINCT将不适用。我认为它不会工作。您缺少第二个表的SELECT。