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

标签 php mysql

我有两张 table

群组

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

最佳答案

您只需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

Demo on SQLFiddle

关于php - 如何获取另一个表 MySQL 查询的组头结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54489215/

相关文章:

javascript - 如何使用 php、AJAX 或 MySQL 使删除按钮完全发挥作用并具有正确的 ID?

php - Aptana 3.0.4 和 PHP 调试

php - JQuery 自动完成搜索术语及其缩写

php - 有没有办法在 SQL 全文搜索中比较特定字段?

javascript - 如何在 WordPress 中通过 javascript 访问 mongoDB 数据库?

php - 使用用户名或电子邮件登录

php - 为什么 PDO 比 mysql_real_escape_string 更适合转义 MySQL 查询/查询字符串?

mysql - JOIN mysql 用于更新表

mysql - ExpressJS - 发送变量作为 POST 请求的响应

php - 自动将具有值的新行添加到从 mysql 生成的 csv 文件