@lexstile

Как доработать SQL-запрос?

Есть запрос:
SELECT DISTINCT events.id, events.name as event_name, events.date_start, events.date_end, events.country_id, events.description, events.day_1, events.day_2, events.month_1, events.month_2, events.year_1, events.year_2, events.is_present, links.links, images.images, countries.countries, countries.country_ids, country_to_cluster.year_start FROM events
		LEFT JOIN (SELECT countries.id, GROUP_CONCAT(countries.id) as country_ids,  event_to_country.country_id as country_id, event_to_country.event_id, GROUP_CONCAT(countries.name) as countries FROM countries LEFT JOIN event_to_country ON countries.id = country_id GROUP BY event_to_country.event_id) as countries ON events.id = countries.event_id
		LEFT JOIN (SELECT event_id, GROUP_CONCAT(link) as links FROM links GROUP BY event_id) AS links ON events.id = links.event_id
		LEFT JOIN (SELECT event_id, GROUP_CONCAT(name) as images FROM images GROUP BY event_id) AS images ON events.id = images.event_id
		LEFT JOIN keywords ON events.id = keywords.event_id
		LEFT JOIN country_to_cluster ON country_to_cluster.country_id = events.country_id AND events.year_1 >= country_to_cluster.year_start AND events.year_1 <= country_to_cluster.year_end
		WHERE (country_to_cluster.cluster_id = :cluster_id) 
		ORDER BY events.year_1 ASC
		LIMIT 30


Можно ли так сделать и как?
Сейчас события выбираются по кластерам, кластер - временной промежуток, есть поля country_to_cluster.year_start и country_to_cluster.year_end.

Возникла необходимость добавления кластера без дат, то есть поля year_start и year_end равны NULL.

Можно ли переделать запрос следующим образом, чтобы он смотрел на даты только в том случае, если они не NULL?

У меня получился такой LEFT JOIN, так?
LEFT JOIN country_to_cluster ON (country_to_cluster.year_start IS NULL AND country_to_cluster.year_end IS NULL && country_to_cluster.country_id = events.country_id) OR (country_to_cluster.country_id = events.country_id AND events.year_1 >= country_to_cluster.year_start AND events.year_1 <= country_to_cluster.year_end)
  • Вопрос задан
  • 86 просмотров
Решения вопроса 1
@MaximaXXl
Такое?
SELECT DISTINCT events.id, events.name as event_name, events.date_start, events.date_end, events.country_id, events.description, events.day_1, events.day_2, events.month_1, events.month_2, events.year_1, events.year_2, events.is_present, links.links, images.images, countries.countries, countries.country_ids, country_to_cluster.year_start FROM events
    LEFT JOIN (SELECT countries.id, GROUP_CONCAT(countries.id) as country_ids,  event_to_country.country_id as country_id, event_to_country.event_id, GROUP_CONCAT(countries.name) as countries FROM countries LEFT JOIN event_to_country ON countries.id = country_id GROUP BY event_to_country.event_id) as countries ON events.id = countries.event_id
    LEFT JOIN (SELECT event_id, GROUP_CONCAT(link) as links FROM links GROUP BY event_id) AS links ON events.id = links.event_id
    LEFT JOIN (SELECT event_id, GROUP_CONCAT(name) as images FROM images GROUP BY event_id) AS images ON events.id = images.event_id
    LEFT JOIN keywords ON events.id = keywords.event_id
    LEFT JOIN country_to_cluster ON country_to_cluster.country_id = events.country_id AND events.year_1 >= coalesce(country_to_cluster.year_start,events.year_1) AND events.year_1 <= coalesce(country_to_cluster.year_end,events.year_1)
    WHERE (country_to_cluster.cluster_id = :cluster_id) 
    ORDER BY events.year_1 ASC
    LIMIT 30


Это будет игнорировать null отдельно для каждого поля year_start и year_end. Если надо именно одновременный null, это будет чуть по другому
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы