我有 5 张 table 。我想使用 MYSQL 查找所有 5 个表的唯一折扣值。
表格的形式是:
table1(userid,discount)
table2(userid,discount)
table3(userid,discount)
table4(userid,discount)
table5(userid,discount)
最佳答案
您可以使用UNION
来实现这一点。
UNION 的默认行为是从结果中删除重复行。可选的 DISTINCT
关键字除了默认值外没有任何作用,因为它还指定重复行删除。
使用可选的 ALL
关键字,不会发生重复行删除,并且结果包括所有 SELECT
语句中的所有匹配行。
查询
select discount from table1
union
select discount from table2
union
select discount from table3
union
select discount from table4
union
select discount from table5;
如果您想对上述 sql 查询的结果进行排序,您可以使用如下所示的 ORDER BY
。
查询
select t.discount from
(
select discount from table1
union
select discount from table2
union
select discount from table3
union
select discount from table4
union
select discount from table5
)t
order by t.discount;
根据您的要求按降序或升序排序。
关于mysql - 如何在多个表中选择唯一的折扣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33471418/