首页 > 解决方案 > 多个表 - 基于 Where 子句的最低值

问题描述

目前有 3 个包含以下列的表,

Table 1
item_id | cost_price

Table 2 
item_id | cost_price

Table 3
item_id | cost_price

需要按子句按item_id排序的三个表之间的最低值排序。item_id 在所有 3 个表之间共享。

我知道它可能是一个带有 INNER 连接的子查询?但是就是不知道怎么写。。。

SELECT table1.cost_price, table2.cost_price, table3.cost_price FROM table2
INNER JOIN table1
ON table1.item_id= table2.item_id
INNER JOIN table3
ON table3.item_id= table1.item_id

我希望按查询中最低的 cost_Price 排序。但我不确定是否应该使用内连接。

标签: sql

解决方案


你在找union all吗?

select item_id, cost_price from table1
union all
select item_id, cost_price from table2
union all
select item_id, cost_price from table3
order by cost_price;

推荐阅读