我有两张桌子,activities
和activity_tours
。 activities
hasManyactivity_tours
与activities.id
>相关activity_tours.activity_id
。我编写了以下 SQL 来选择两个表中具有两个条件之一的行。
- 如果它有一定的
activity_tours.tour_type
OR - 该活动有不止一次旅行
该查询似乎有点骇人听闻。有人可以帮我提高这个查询的效率。
SELECT SQL_CALC_FOUND_ROWS
activities.activity_name,
activities.image_path,
activity_tours.tour_type
FROM
activities
LEFT JOIN activity_tours ON activities.id = activity_tours.activity_id
WHERE
(
activity_tours.tour_type != 17 OR (
(SELECT count(*) FROM activity_tours WHERE activity_id = activities.id) > 1)
)