查询一行形成mysql的多个相同行

时间:2016-06-02 07:30:38

标签: php mysql

我有4个表,例如roomtype,hotel,available和rimage.Roomtype有4行for room.Hotel有一行。可用于今天的4行日期。 但是对于一个房型的行来说有很多行。

SELECT * 
  FROM roomtype
  LEFT JOIN hotel
    ON roomtype.hid = hotel.hid
  LEFT JOIN available
    ON roomtype.rid = available.rid
  LEFT JOIN rimage
    ON roomtype.rid = rimage.rid
 WHERE available.adt = '$today'

当这些查询运行时,我从rimage获取了很多行。但是我想为每个roomtype&row的行只得到一行。如何为这些解决方案编写查询。

1 个答案:

答案 0 :(得分:0)

试试这个

SELECT * 
FROM roomtype 
LEFT JOIN hotel ON roomtype.hid = hotel.hid 
LEFT JOIN available ON roomtype.rid = available.rid 
INNER JOIN (SELECT MIN(rid) FROM rimage WHERE roomtype.rid = rimage.rid) b ON roomtype.rid = b.rid  
where available.adt = '$today'

或者,如果房型没有必须有rimage

SELECT * 
FROM roomtype 
LEFT JOIN hotel ON roomtype.hid = hotel.hid 
LEFT JOIN available ON roomtype.rid = available.rid 
LEFT JOIN (SELECT MIN(rid) FROM rimage WHERE roomtype.rid = rimage.rid) b ON roomtype.rid = b.rid  
where available.adt = '$today'