iOSGeek
iOSGeek

Reputation: 5577

Join query in Mysql of two tables

I have two tables :

ad ( id(int) , userid(int) ) : table to store advertisement which have a user column
adcat ( adid(int) , catid(int) ) : table to store the category id (catid) of each ad (adid)

now all I want is to get the number of ads of a specific category published by a specific user

example : all the ads of catid = 1 and with userid = 725

Thank you

Upvotes: 1

Views: 68

Answers (3)

peterm
peterm

Reputation: 92785

Try use JOIN and COUNT

SELECT COUNT(*) ads_total
  FROM ad a JOIN
       adcat c ON a.id = c.adid
 WHERE c.catid = 1 AND
       a.userid = 725

SQLFiddle

Upvotes: 2

luksch
luksch

Reputation: 11712

you need to join the tables:

SELECT * FROM ad INNER JOIN adcat ON adid=id WHERE catid=1 and userid=725;

Upvotes: 1

everending
everending

Reputation: 31

SELECT * FROM ad 
LEFT JOIN adcat 
ON adcat.adid = ad.id
WHERE adcat.catid = 1 AND ad.userid = 725

Upvotes: 3

Related Questions