我不确定为什么这不起作用。如果我单独加入任一表,它会返回相应的结果,但当我尝试将它们加入时,我得到0结果。 (car_id和boat_id都是他们桌子上的主键。)
$query = "SELECT
*
FROM
posted c
JOIN posted_car e on c.car_id = e.car_id
JOIN posted_boat g on c.boat_id = g.boat_id
WHERE
c.posted = 'posted'
ORDER BY date DESC LIMIT 0, 30";
$resultBoth = mysql_query($query, $db) or die(mysql_error($db));
当我做的时候可能值得注意
LEFT JOIN posted_car e on c.car_id = e.car_id
RIGHT JOIN posted_boat g on c.boat_id = g.boat_id
我得到的结果好像我只加入了publish_boat表。如果有人能指出我正确的方向......我将不胜感激。
答案 0 :(得分:2)
您正在使用可能有问题的JOIN。你应该使用左外连接来获得正确的结果。检查以下语法:
$query = "SELECT *
FROM
posted c
left OUTER JOIN posted_car e on c.car_id = e.car_id
left OUTER JOIN posted_boat g on c.boat_id = g.boat_id
WHERE c.posted = 'posted'
ORDER BY date DESC LIMIT 0, 30";
$resultBoth = mysql_query($query, $db) or die(mysql_error($db));