So, I am trying to modify the following query:
SELECT *
FROM flow
INNER JOIN flow_strings
USING(node_id)
WHERE
(
flow.parent = 0
OR flow.parent = :user_flow
)
AND flow.source = 0
AND :input LIKE CONCAT('%', flow_strings.sql_regex, '%')
ORDER BY flow.parent DESC, RAND()
The query works exactly how it’s supposed to. HOWEVER, I need to modify it a bit. Assuming a priority INT column on table flow, how can I modify it to only grab results where flow.priority is equal to MAX(priority)?
In other words, I want the query to do exactly what it does, but in the event that there are priority values, I want it to only select from the highest priorities. If there is more than one high priority value, then it should select both.
I have seen some other posts on StackOverflow that appear to be asking the same question, but the answers don’t seem to explain the solution–they only post the answer. If you could attach an explanation of what’s going on, I’d hugely appreciate it! Thanks!
One way to solve this is using a subquery:
This way you select only rows with the biggest priority among those who satisfy the grouping condition. Generally this one is an equality between one or more fields from the main table and the corresponding fields in the subquery. For example in your case could be:
(this is my guess, I don’t know exactly the meaning of your tables.