Satch3000
Satch3000

Reputation: 49384

PHP SQL Request - Query 2 tables, for results on one query

I need to query the database to pull all cars that a particular user likes.

I know that user_id is 1

Then I have 2 tables. 1 contains all the cars ... id and description and table 2 contains the likes.

Table 1 has a list if cars and these fields:

car_id
car_name,
car_description


Table 2 has what cars I like and these fields:

user_id
car_id
likes (1 or 0)

So I need to pull out only the records that user 1 likes from Table 2 but only the ones he likes

What SQL query would I need to do for that?

Upvotes: 0

Views: 46

Answers (3)

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

Try this query

SELECT t1.*,t2.*
FROM tbl1 t1,tbl2 t2
WHERE likes = 1 AND user_id = 1 AND t1.car_id = t2.car_id

Upvotes: 0

Modestas Stankevičius
Modestas Stankevičius

Reputation: 144

SELECT * FROM table1 as t0
LEFT JOIN table2 as t1 on t0.car_id = t1.car_id
WHERE t1.likes = 1

Upvotes: 2

user2401931
user2401931

Reputation:

Try this

SELECT * FROM table1 as t1 LEFT JOIN table2 as t2 on t1.car_id = t2.car_id WHERE t2.user_id = $user_id

Upvotes: 1

Related Questions