我需要MySQL查询的帮助。我有2个表,clients
和payments
。表格clients
有2个字段id
和name
。表格payments
包含id
,id_clients
,pay_month
。客户需要每月支付一定金额。
我想只搜索尚未支付当月的客户名称。
我的查询
SELECT name FROM clients WHERE id NOT IN (SELECT DISTINCT id_client FROM payments WHERE pay_month > '2014-03-15' ORDER BY pay_month DESC)
我设法解决了我的疑问。
我用php来存储日期$today = date("Y-m-d");
SELECT DISTINCT clients.id, clients.name FROM clients WHERE clients.id NOT IN (SELECT DISTINCT id_clients FROM payments WHERE pay_month > '".$today."' ORDER BY pay_month DESC) ORDER BY clients.name ASC
谢谢大家的帮助。
答案 0 :(得分:0)
也许是这样的。
SELECT
c.name as client_name,
p.pay_month as payment_month
FROM clients c
LEFT JOIN payments p on p.id_clients = c.id
WHERE p.id IS NULL AND YEAR(p.pay_month) >= YEAR(NOW()) AND MONTH(p.pay_month) > MONTH(NOW());
如果你能给我们一些样本数据,我可以建立一个sql小提琴,并且更容易做到。 刚编辑修复日期格式/检查