L
L
lexstile2020-08-27 00:47:02
MySQL
lexstile, 2020-08-27 00:47:02

How to properly refine a SQL query?

There is a request:

SELECT 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, countries.name as country_name, links.links, images.images, clusters.clusters FROM events
  LEFT JOIN countries ON events.country_id = countries.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 (SELECT country_id as c_id, GROUP_CONCAT(name) as clusters FROM clusters GROUP BY c_id) AS clusters ON countries.id = c_id
  WHERE (events.name LIKE :search || events.description LIKE :search) 
  ORDER BY events.year_1 ASC
  LIMIT 100

I would also like to search for events by keywords that are in another table:
5f46d7ed225eb020496762.png
That is, the condition should be of the form: WHERE (events.name LIKE :search || events.description LIKE :search || keywords.name LIKE :search)

As correct refine the request and achieve the desired result?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Vodakov, 2020-08-27
@lexstile

Add join:

LEFT JOIN keywords ON keywords.event_id = events.id

and then your new condition will work
To avoid duplicates in the selection results (if there are matches for several keywords, they may appear) use SELECT DISTINCT

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question