Answer the question
In order to leave comments, you need to log in
How to intelligently facilitate Select to select multiple values that satisfy a condition?
When fetching data from a table by condition with join, I encountered the fact that the query execution time is quite large. As a result, I get an array of several hundred records, but I only need to display 7. How to correctly build a query so that the first seven records found that meet a certain condition and have the maximum value of a certain field are selected?
I so understand that LIMIT for selection of 7 found records does not approach; for it you need to specify the position, and in my case the position is not known.
SELECT `gkinfo`.`id`, `title`
FROM `ga_keyword_info` `gkinfo`
JOIN `ga_keyword` `gk` ON gkinfo.keyword_id=gk.id
WHERE `gk`.`campaign_id` IN ('1', '2', '3' и тд..);
Answer the question
In order to leave comments, you need to log in
SELECT `gkinfo`.`id`, `title`
FROM `ga_keyword_info` `gkinfo`
JOIN `ga_keyword` `gk` ON gkinfo.keyword_id=gk.id
WHERE `gk`.`campaign_id` IN ('1', '2', '3' и тд..)
ORDER BY someField desc limit 7;
--где someField - поле по которому нужно сортировать(desc(по убыванию) - от большего к меньшему, то есть выберет 7 записей с максимальными значениями поля someField
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question