0

我的查询是这样的......

SELECT tb1.name, COUNT(tb2.payment_date) as first_payment FROM table1 LEFT JOIN table2 tb2 ON tb2.id_user = tb1.id

+-----------+-------------+
| Name      |  Count      |
+-----------+-------------+
| John      |   543534543 |

但我希望我的查询返回连接限制,例如:

LEFT JOIN tb2 ON tb2.id_user = tb1.id { LIMIT JOIN }

只返回一个关系......

+-----------+-------------+
| Name      |  Count      |
+-----------+-------------+
| John      |   3         |
4

2 回答 2

1
select tb1.name, count(*) as first_payment
from table1 t1
LEFT JOIN (SELECT id_user, min(payment_date) 
           FROM table2
           GROUP BY id_user) as t2
ON t1.id = t2.id_user
GROUP BY tb1.name
于 2013-01-17T12:48:01.220 回答
0
SELECT 
    tb1.name, 
    COUNT(tb2.payment_date) as first_payment 
FROM table1 
LEFT JOIN (SELECT id_user , MIN(id) FROM table2 GROUP BY id_user)as tb2 ON tb2.id_user = tb1.id
于 2013-01-17T12:43:19.557 回答