A
A
alex5e2015-01-05 14:58:03
MySQL
alex5e, 2015-01-05 14:58:03

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

2 answer(s)
I
IceJOKER, 2015-01-05
@alex5e

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

A
Andrew, 2015-01-15
@psykrab

but it just
doesn't fit?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question