我在这里生气...
我想
SELECT `id` FROM `colors`
WHERE `colors.id=1`
AND `cars.user=1`
AND `colors.car=cars.id`
来自我的表格
users
id, name
1, Nicolas
2, John
cars
id, user, name
1, 1, Mercedes
2, 2, Ferrari
colors
id, car, name
1, 1, Black
2, 2, Red
这个数据库意味着,尼古拉斯有黑色梅赛德斯和约翰有红色法拉利
例如,如果我使用带有WHERE colors.id=1
AND cars.user=2
的SELECT语句,则需要返回一个空结果。
答案 0 :(得分:0)
使用JOIN从所有表中获取结果。
SELECT u.name AS UserName, c.name AS CarName, col.name AS CarColor FROM users AS u
JOIN cars AS c ON (c.user = u.id)
JOIN colors AS col on (col.car = c.id)
WHERE u.id = 1;