0

I would like to get the data from one table, and count all results from other table, depending on the first table data, here is what I tried:

SELECT
    cars.*, (
        SELECT
            COUNT(*)
        FROM
            uploads
        WHERE
            uploads.cid = cars.customer
    ) AS `count`,
FROM
    `cars`
WHERE
    customer = 11;

I dont really have an idea why its not working, as I'm not a regular MySQL user/coder...

Could anyone direct me in the right direction with this one?

4

4 回答 4

1
SELECT
    c.*, COUNT(u.cid) AS count
FROM
    cars c
LEFT JOIN 
    uploads u
ON
    u.cid=c.customer
WHERE
    u.customer = 11;
GROUP BY c.cid
于 2012-10-25T05:31:53.310 回答
1

通过使用连接两个表来尝试LEFT JOIN

SELECT  a.customer, COUNT(b.cid) totalCount
FROM    cars a
        LEFT JOIN uploads b
            ON a.customer = b.cid
WHERE   a.customer = 11
GROUP BY a.customer

使用COUNT(*)inLEFT JOIN将使记录的最小计数为 1。

于 2012-10-25T05:30:53.027 回答
0
SELECT  cars.*,COUNT(uploads.*) as uplloaded 
from cars 
left outer join uploads on  uploads.cid = cars.customer
where  cars.customer = 11 
group by uploads.cid;
于 2012-10-25T05:32:15.680 回答
0

尝试这个 :

SELECT  customer, COUNT(cid) totalCount
FROM    cars 
        INNER JOIN uploads 
            ON (customer = cid)
WHERE   customer = 11
GROUP BY customer
于 2012-10-25T05:32:33.673 回答