如何获取组头结果另一个表MySQL查询

时间:2019-02-02 01:39:45

标签: php mysql

我有两个桌子

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

trans

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

1 个答案:

答案 0 :(得分:1)

您只需要JOIN两个表并检查给定交易的帐户headCashInHand

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

enter image description here