mysql - Joining two tables without a common column but using another table connected to both
问题描述
I have three tables in Mysql
- table A-> uid, itemType, balance,orderType
- table B-> orderId, itemType, qty
- table C-> uid,orderId
I need to get (balance-qty) for every uid:orderIdcombination for specific type of item. How can I connect table A and table B if they have no common column to join on. But both of them are connected to table C.
解决方案
With joins between the 3 tables:
select a.uid, a.balance, b.qty
from tablea a
inner join tablec c on c.uid = a.uid
inner join tableb b on b.orderid = c.orderid
where a.itemtype = ?
You can add more columns if you need them.
推荐阅读
- java - 如何解决“指定的 Gradle 发行版‘https://services.gradle.org/distributions/gradle-4.10.1-all.zip’不存在。”
- python - Python 3 内置文件写入方法似乎不适用于 '\n' 换行
- python - 使用 python 进行 Azure AD 验证
- wordpress - 如何为用户未设置的帖子设置缩略图
- android - Google 应用签名、多个应用和 OAuth 客户端 ID:用于 Google API:s 问题
- html - 箭头动画移动侧问题
- javascript - 是否可以使用 JavaScript 在 CRM 的 OOB 查找对话框窗口中获取视图的名称或 ID
- xamarin.forms - Auth0 - 更新用户的根属性 - 403 错误:“您无法更新以下字段”
- jquery - 在 Tempus Dominus Bootstrap 4 datetimepicker 中选择日期后以编程方式切换到时间视图?
- mysql - 如何更新多个相同的外键值,这是laravel中的文件类型