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?

1
  • is it throwing any exception ? Commented Oct 25, 2012 at 5:32

4 Answers 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
Sign up to request clarification or add additional context in comments.

Comments

1

Try it by joining both tables using 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

using COUNT(*) in LEFT JOIN will have records to have a minimum count of 1.

1 Comment

This one selects only one record from the cars table. :/
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;

Comments

0

Try this :

SELECT  customer, COUNT(cid) totalCount
FROM    cars 
        INNER JOIN uploads 
            ON (customer = cid)
WHERE   customer = 11
GROUP BY customer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.