限制内连接表上的一个结果

时间:2012-11-03 02:23:59

标签: mysql join limit

您好我有以下问题:

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'"

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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