首页 > 解决方案 > 如何获取组头结果另一个表 MySQL 查询

问题描述

我有两张桌子

团体

gid| account         | head
---------------------------------
1  | first cash      | CashinHand
2  | second cash     | CashinHand
3  | third  cash     | CashinHand
4  | office expense  | Expense

反式

tid| particular      | amount
------------------------------
1  | first cash      | 100
2  | office expense  | 300
3  | second cash     | 130
4  | third  cash     | 50
5  | first cash      | 110
6  | office expense  | 750

// 我想要获取报告所有现金。它取决于CashinHand 这样的组表头

tid| particular      | amount
------------------------------
1  | first cash      | 100
3  | second cash     | 130
4  | third  cash     | 50
5  | first cash      | 110

标签: phpmysql

解决方案


您只需要查看JOIN您的两个表并检查head给定交易的帐户是否为CashInHand

SELECT t.*
FROM trans t
JOIN `group` g ON g.account = t.particular
WHERE g.head = 'CashInHand'

输出:

tid     particular      amount
1       first cash      100
3       second cash     130
4       third cash      50
5       first cash      110

SQLFiddle 上的演示


推荐阅读