1

我有两张桌子:

  • bpoint-> 所有房屋的表格
  • images-> 房屋图片表(每栋房屋最多 3 张)。

这是我的 MySQL 连接查询,用于获取房屋信息及其图像:

SELECT 
    bpoint.*, bimages.iURL
FROM
    bpoint,
    bimages
WHERE
    bpoint.pointID  = bimages.pointID
AND bimages.pointID = '$tocka'

由于图像行多于房屋行(始终只有一个房屋结果和 1-3 个图像结果),有没有办法删除每个图像附带的房屋结果双倍?

4

2 回答 2

1

通过使用GROUP BYand aJOIN你可以得到你想要的结果;

SELECT bpoint.*, bimages.url FROM bpoint
LEFT JOIN bimages 
       ON bpoint.id = bimages.pointId
GROUP BY bpoint.id;

http://sqlfiddle.com/#!8/5d1fd/3/0

于 2012-09-03T14:13:22.780 回答
1

不,没有办法做到这一点,除非您使用两个单独的查询。如果有办法,您希望结果集看起来像第一所房子的第二张图片吗?

Anyway, what I would do here is add an ORDER BY clause to the query, to make sure all images from the same house are in sequence. Then, on the PHP display loop you just ignore the house information if you already printed it from the previous row.

于 2012-09-03T14:17:44.653 回答