您好,我的数据库中有两个表,客户和商店。我想获得所有客户并澄清客户是否是债务人。对于这项工作,我想创建一个 mysql 查询来获取客户并加入商店表。我想为 if shops.price_statuscolumn = 1 come 和 shops 表中的总价格值设置条件。否则,商店表中价格列的那些行的值等于 0。我的商店表结构和一些示例数据id customer_id product_id price price_status1 81 12 300 12 81 12 100 03 81 15 200 14 90 10 600 05 90 15 50 16 63 16 10 0还有我的客户表(你应该知道有些客户不购买任何产品,因为我想看到他们)id name63. Eva64. Nva81. Ali82. Bill90. Mosh我想得到这个结果Eva. 0. // Because price status = 0Nva. 0. Ali. 500 // because have two row that prices status = 1Bill. 0. Mosh. 50. // Because have one row with price status = 1
1 回答
ibeautiful
TA贡献1993条经验 获得超5个赞
这可以做[MySQL if][1]
SELECT
c.`name`,sum(if(s.price_status=1,s.price,0)) as price
FROM
customer AS c
LEFT JOIN
shops AS s
ON
c.id=s.customer_id
GROUP BY
c.id;
如果你接受 null 值,price你可以使用
SELECT
c.`name`,sum(s.price_status*s.price) as price
FROM
customer AS c
LEFT JOIN
shops AS s
ON
c.id=s.customer_id
GROUP BY
c.id;
- 1 回答
- 0 关注
- 115 浏览
添加回答
举报
0/150
提交
取消