SQL Select 和从多个表中排序
SQL Select and Sort from Multiple tables
我有三个table:
Table衣服
|id | productname | manufacturer | arrivaldate |
+----+-------------+-----------------+--------------+
| 1 | Shirt | MfrA | 5/30/20 |
| 2 | Tshirt | MfrB | 3/17/20 |
Table玩具
|id | productname | manufacturer | arrivaldate |
+----+-------------+------------------+--------------+
| 1 | Car | MfrC | 2/16/19 |
| 2 | Ball | MfrD | 8/25/20 |
Table 工具
|id | productname | manufacturer | arrivaldate |
+----+-------------+------------------+--------------+
| 1 | Hammer | MfrE | 4/17/20 |
| 2 | Drill | MfrF | 9/12/21 |
我想显示 5 latest/newest 个产品,每个 table 都包含,按到货日期降序排列。
它可以是一个新的数据库视图或 Table.
期望的输出是这样的:
最新产品到货
| productname | manufacturer | arrivaldate |
+-------------+------------------+--------------+
| Drill | MfrF | 9/12/21 |
| Ball | MfrD | 8/25/20 |
| Shirt | MfrA | 5/30/20 |
对此 SQL 查询是什么?
实际上,另一个人有答案并删除了它,这也是我会提供的。唯一的区别是 SQL-Server 允许使用“TOP”关键字,而 MySQL 使用“LIMIT”。
通过单独执行每个 table,您可以按到达日期降序排列,从而将最新的条目浮动到列表的顶部。然后,应用 LIMIT 5 会将结果限制为仅返回排序结果后的前 5 个。
通过对每个 table 来源(衣服、玩具、工具)进行 UNION,概念是相同的。我只是添加了一个 free-form 描述,所以你知道它起源于哪个 table。
UNION 只要求每个 table 结果具有相同的列和列数。在这种情况下,它们都是相同的,并且也是相同的名字。
select * from
( select
'Clothes' as TableSource,
id,
productname,
manufacturer,
arrivaldate
from
Clothes
order by
arrivaldate desc
limit 5 ) QryClothes
UNION
select * from
( select
'Toys' as TableSource,
id,
productname,
manufacturer,
arrivaldate
from
Toys
order by
arrivaldate desc
limit 5 ) QryToys
UNION
select * from
( select
'Tools' as TableSource,
id,
productname,
manufacturer,
arrivaldate
from
Tools
order by
arrivaldate desc
limit 5 ) QryTools
如果 display 5 latest/newest products, included from each table
是指组合结果集中的最新 5 个,则使用 UNION 的视图将完成这项工作:
create view testvw as select * from
(
(select productname,manufacturer,arrivaldate from Clothes)
UNION
(select productname,manufacturer,arrivaldate from Toys)
UNION
(select productname,manufacturer,arrivaldate from Tools)
) x
order by arrivaldate desc limit 5;
我有三个table:
Table衣服
|id | productname | manufacturer | arrivaldate |
+----+-------------+-----------------+--------------+
| 1 | Shirt | MfrA | 5/30/20 |
| 2 | Tshirt | MfrB | 3/17/20 |
Table玩具
|id | productname | manufacturer | arrivaldate |
+----+-------------+------------------+--------------+
| 1 | Car | MfrC | 2/16/19 |
| 2 | Ball | MfrD | 8/25/20 |
Table 工具
|id | productname | manufacturer | arrivaldate |
+----+-------------+------------------+--------------+
| 1 | Hammer | MfrE | 4/17/20 |
| 2 | Drill | MfrF | 9/12/21 |
我想显示 5 latest/newest 个产品,每个 table 都包含,按到货日期降序排列。 它可以是一个新的数据库视图或 Table.
期望的输出是这样的:
最新产品到货
| productname | manufacturer | arrivaldate |
+-------------+------------------+--------------+
| Drill | MfrF | 9/12/21 |
| Ball | MfrD | 8/25/20 |
| Shirt | MfrA | 5/30/20 |
对此 SQL 查询是什么?
实际上,另一个人有答案并删除了它,这也是我会提供的。唯一的区别是 SQL-Server 允许使用“TOP”关键字,而 MySQL 使用“LIMIT”。
通过单独执行每个 table,您可以按到达日期降序排列,从而将最新的条目浮动到列表的顶部。然后,应用 LIMIT 5 会将结果限制为仅返回排序结果后的前 5 个。
通过对每个 table 来源(衣服、玩具、工具)进行 UNION,概念是相同的。我只是添加了一个 free-form 描述,所以你知道它起源于哪个 table。
UNION 只要求每个 table 结果具有相同的列和列数。在这种情况下,它们都是相同的,并且也是相同的名字。
select * from
( select
'Clothes' as TableSource,
id,
productname,
manufacturer,
arrivaldate
from
Clothes
order by
arrivaldate desc
limit 5 ) QryClothes
UNION
select * from
( select
'Toys' as TableSource,
id,
productname,
manufacturer,
arrivaldate
from
Toys
order by
arrivaldate desc
limit 5 ) QryToys
UNION
select * from
( select
'Tools' as TableSource,
id,
productname,
manufacturer,
arrivaldate
from
Tools
order by
arrivaldate desc
limit 5 ) QryTools
如果 display 5 latest/newest products, included from each table
是指组合结果集中的最新 5 个,则使用 UNION 的视图将完成这项工作:
create view testvw as select * from
(
(select productname,manufacturer,arrivaldate from Clothes)
UNION
(select productname,manufacturer,arrivaldate from Toys)
UNION
(select productname,manufacturer,arrivaldate from Tools)
) x
order by arrivaldate desc limit 5;