0

我需要查询数据库以提取特定用户喜欢的所有汽车。

我知道 user_id 是 1

然后我有2张桌子。1 包含所有汽车 ... id 和描述,表 2 包含喜欢。

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)

所以我只需要从表 2 中提取用户 1 喜欢的记录,而只提取他喜欢的记录

我需要为此执行什么 SQL 查询?

4

3 回答 3

2
SELECT * FROM table1 as t0
LEFT JOIN table2 as t1 on t0.car_id = t1.car_id
WHERE t1.likes = 1
于 2013-06-07T10:53:56.063 回答
1

尝试这个

SELECT * FROM table1 as t1 LEFT JOIN table2 as t2 on t1.car_id = t2.car_id WHERE t2.user_id = $user_id
于 2013-06-07T10:54:47.020 回答
0

试试这个查询

SELECT t1.*,t2.*
FROM tbl1 t1,tbl2 t2
WHERE likes = 1 AND user_id = 1 AND t1.car_id = t2.car_id
于 2013-06-07T10:55:18.983 回答