sql - 多个表 - 基于 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 排序。但我不确定是否应该使用内连接。
解决方案
你在找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;
推荐阅读
- ios - XCTestCase 异步
- python - 读取 tsv 列并存储在字典中,结果为 None 值
- android - 如何在 Android Studio 中制作具有相对布局、Framelayout、NavigationBarLayout、可滚动的 Activity?
- omnet++ - 如何将 moveToXY() 方法插入静脉?
- javascript - 我试图弄清楚如何使用函数在对象中添加值
- curl - 来自服务器的卷曲空回复,代码为 100
- php - Summernote 编辑器 - 上传后无法查看图像
- c# - 消息:错误代码 -3:Aerospike C# 客户端返回的节点无效
- php - 表格内的表格与 POST 数组交互
- file - 从 filename.zip 复制修改日期并覆盖 .ini 文件中的现有日期