我对MySQL查询有疑问,我想获取与相同FK关联的所有行。 代码是:
$idad =(int)$_GET['id'];// I am using this to get the id of ad from the url of page
$sql = 'SELECT * FROM ads INNER JOIN cars ON cars.ad_id="'.$idad.'"
INNER JOIN address ON address.ad_id="'.$idad.'" ';
我仅对ads表有错误的结果 ,它返回了错误的ad_id行。但是对于地址和汽车,我得到了正确的行。
答案 0 :(得分:2)
您必须将ad_id与其他表格开始键广告ID映射。
$sql = SELECT * FROM ads
INNER JOIN cars ON cars.ad_id=ads.ad_id
INNER JOIN address ON address.ad_id=ads.ad_id
WHERE ads.ad_id = $idad
答案 1 :(得分:0)
我通过编辑SQL查询来解决:
$sql = 'SELECT * FROM ads JOIN cars ON cars.ad_id="'.$idad.'" AND ads.ad_id="'.$idad.'"
JOIN address ON address.ad_id="'.$idad.'" ';
现在运行良好。
答案 2 :(得分:0)
您要从ads表中选择所有内容,但必须使用join。
$idad =(int)$_GET['id'];
$sql = 'SELECT * FROM ads INNER JOIN cars ON cars.ad_id=address.ad_id
INNER JOIN address ON address.ad_id=ads.ad_id WHERE ad_id=$idad';