2

您好我有以下查询:

SELECT pr.*, img.image_file, c.contract_id, c.cmin_markup, c.cmeal_plan, c.tax_include, c.civa, m.plan_name, r.room_name, r.room_id FROM contracts AS c
INNER JOIN meal_plan AS m ON m.plan_code = c.cmeal_plan AND m.lang = '1'
INNER JOIN room_infos AS r ON r.hotel_id = '$hid' AND r.lang = '1'

LEFT JOIN images AS img ON img.foreign_id = r.room_id LIMIT 1

LEFT JOIN promotions as pr ON pr.hotel_id = '$hid' AND FIND_IN_SET('c.contract_id', pr.contract_id) > 0 AND FIND_IN_SET('r.room_id', pr.room_id) > 0 AND pr.travel_start <= '$now' AND pr.travel_end >= '$now' AND pr.book_start <= '$arriving' AND pr.book_end >= '$departing' AND pr.lang='1'
WHERE c.hotel_id = '$hid' AND c.cstart <= '$arriving' AND c.cend >= '$departing' AND c.ctype = '1'"

我需要限制图像表只给我一个结果。我不确定这是否可行,或者如何实现这一目标的最佳方式。如果需要更多信息,请告诉我。要发布的表格很多。提前感谢您的帮助!!

4

2 回答 2

2

在查询末尾包含一个分组依据。假设您的图像表有一个 ID 字段,只需附加“GROUP BY images.id”

于 2012-11-03T04:34:29.550 回答
0

我面前没有 mySql 实例来尝试这个,但考虑将图像移动到子查询中:

LEFT JOIN (select * from images LIMIT 1) AS img on img.foreign_id = r.room_id

这当然不是很有效,因为内联子查询中没有 where 子句,您可以改进子查询以使其更具限制性并将其与父查询连接。

像这样的东西:

select * from images as img2 where img2.foreign_id = r.room_id LIMIT 1
于 2012-11-03T02:32:16.743 回答