Php 获取表中的所有记录
我想检索所有“金额”大于或等于1500的记录。问题是,即使“金额”小于1500,也会显示在页面中Php 获取表中的所有记录,php,mysql,sql,phpmyadmin,Php,Mysql,Sql,Phpmyadmin,我想检索所有“金额”大于或等于1500的记录。问题是,即使“金额”小于1500,也会显示在页面中 customers table id name 1 sample 2 sample2 3 sample3 payments table p_id amount id(foreign key) 1 800 2 2 800 2 3 1500 1 4 1200 3 应检索客户1和客户2,因为金额>=150
customers table
id name
1 sample
2 sample2
3 sample3
payments table
p_id amount id(foreign key)
1 800 2
2 800 2
3 1500 1
4 1200 3
应检索客户1和客户2,因为金额>=1500
谢谢,,
米克:)这需要加入表<使用code>GROUP BY是因为使用
SUM()
对其中一列进行聚合,并且使用HAVING
子句过滤聚合结果
SELECT a.ID, a.name
FROM customers a
INNER JOIN payments b
ON a.ID = b.id
GROUP BY a.ID, a.name
HAVING SUM(b.amount) >= 1500