I have 3 tables as described below ,
table1 'ads'
id userid
1 47
2 47 //ads.id = adcat.adid
3 45
4 47
table2 'adcat'
adid catid
1 1
2 3
3 3 // adcat.catid = categories.id
4 3
table3 'categories'
id name
1 mathematic
2 biolog
3 leteratur
4 chemi
What I want is to get categories.id from ads.id.
I have tried this
SELECT categories.id, ads.id
FROM ads
INNER JOIN adcat ON ads.id = adcat.adid
INNER JOIN categories ON categories.id = adcat.catid
But it does not come by the exact value.
obs : I don’t get any error just not exact value.
any help will be much appreciated.
Seems to be no reason to go in to categories table if adcat.catid = categories.id and that is the only value you need.
Use
$row['catid']with this query.If in the future you need more columns from categories then you will need a double join:
Use
$row['id']with this query.See
*AS id*and*AS ads_id*– remove the stars and you are giving the columns anALIASwhich is basically like a nickname, this can be handy in many cases but especially important in this case since the two columns both have the same names. So now you can call$row['id']and get categories.id