2010-01-17 26 views
7

Ho una tabella nel mio DB denominata "studenti" contiene le seguenti colonne (student_id, student_name, year_of_birth) .e serie di anni Ho provato a creare una query che ottiene 10 id_sidile di ciascuna anno in (anni) array.limite multiplo mysql in una istruzione sql

Potrei scrivere

SELECT student_id FROM `students` WHERE year_of_birth=1950 LIMIT 10; 
SELECT student_id FROM `students` WHERE year_of_birth=1951 LIMIT 10; 
SELECT student_id FROM `students` WHERE year_of_birth=1952 LIMIT 10; 
SELECT student_id FROM `students` WHERE year_of_birth=1953 LIMIT 10; 
(and so on) 

ma sarebbe molto che richiede tempo Ci sono altre opzioni Thank you

risposta

1

Se la preoccupazione è che le query torneranno più set di risultati, si potrebbe buttare un UNION ALL tra ogni SELECT:

SELECT student_id FROM `students` WHERE year_of_birth=1950 LIMIT 10 
UNION ALL 
SELECT student_id FROM `students` WHERE year_of_birth=1951 LIMIT 10 
UNION ALL 
... 

Questo può di Ovviamente si combina l'approccio di alexn per generare la query da una serie di anni.

I non penso che questo fornirà prestazioni molto migliori rispetto a query separate, ma potrebbe (in una futura versione di MySQL) poiché fornisce al motore del database un po 'più di informazioni su ciò che si sta facendo.

0

Utilizzare un sub-query che collega tornare al tavolo:

SELECT student_id FROM `students` AS s1 
WHERE student_id IN 
    (SELECT s2.student_id FROM `students` AS s2 
    WHERE s1.year_of_birth = s2.year_of_birth 
    LIMIT 10) 

Solo un problema però: questo funziona solo se si utilizzare MySQL 5.1 o versioni successive.

L'alternativa è quella di utilizzare una dichiarazione dell'Unione:

for ($year = 1950; $year < 2000; $year++) { 
    $stmts[] = "SELECT student_id FROM `students` 
       WHERE year_of_birth = $year LIMIT 10"; 
} 
$sql = implode(' UNION ALL ', $stmts; 

Questo lavoro per una più ampia gamma di versioni di MySQL.

0

Questo è anche uno degli esempi per trovare anno bisestile con più limiti

select year_n from the_years 

select distinct month_n from the_months,the_years where year_n=$P{Year} 

(select distinct day_n from the_days,the_months where $P{Month} IN('Jan','Mar','May','Jul','Aug','Oct','Dec') limit 31) 
UNION ALL 
(select distinct day_n from the_days,the_months where $P{Month} IN('Apr','Jun','Sep','Nov') limit 30) 
UNION ALL 
(select distinct day_n from the_days,the_years,the_months where $P{Month}='Feb' and mod($P{Year},4)!=0 or mod($P{Year},100)=0 or mod($P{Year},400)=0 limit 28) 
UNION ALL 
(select distinct day_n from the_days,the_years,the_months where $P{Month}='Feb' and mod($P{Year},4)=0 and mod($P{Year},100)!=0 or mod($P{Year},400)=0 limit 29) 
0

perché non semplicemente fare questo:

$studentIds = array(1950, 1951, 1952, 1953); 

$sql = " 
    SELECT 
     student_id, 
     year_of_birth 
    FROM 
     students 
    WHERE 
     student_id IN (" . implode(',', $studentIds) . ") 
"; 

$result = mysql_query($sql); 

$students = array(); 
while($row = mysql_fetch_assoc($result)) { 
    $students[$row['year_of_birth']] = $row['student_id']; 
} 

vostro array $students conterrà array di id degli studenti con i tasti come gli anni della nascita.

+0

se sei più preoccupato del limite, puoi limitarlo in php, con un contatore. –