bir SQL deyiminde birden sınırını mysql

5 Cevap php

I have table in my DB called "students" contains the following columns (student_id, student_name, year_of_birth).and array of years I trying to make one query that gets 10 student_id of each year in (years) array.

Ben yazabilirsiniz

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)

But that would be very time consuming Are there any other options Thank you

5 Cevap

"MySQL bir grup N satırları seçmek": http://explainextended.com/2009/03/06/advanced-row-sampling/

Eğer endişe sorguları birden çok sonuç kümeleri dönecektir ise, her birinin arasına bir UNION ALL atmak SELECT olabilir:

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
...

Bu, tabii ki yıllık bir diziden sorgu üretme alexn yaklaşımı ile kombine edilebilir.

Bu veritabanı motoru ne yapıyorsun hakkında biraz daha bilgi verir beri not Bu size ayrı sorgular çok daha iyi performans verecektir sizce, ama bu (MySQL gelecek sürümünde) olabilir.

Masaya geri bağlayan bir alt sorgu kullanın:

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)

Ama tek sorun: Eğer MySQL 5.1 veya daha yüksek kullanırsanız bu sadece çalışır.

Alternatif bir sendika deyimi kullanmaktır:

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

Bu MySQL sürümleri geniş bir yelpazede için çalışacaktır.

Bu, birden sınırları ile artıkyıl bulmak için de örnek biri

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) 

neden sadece bunu değil:

$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'];
}

Sizin $students dizisi doğum yıl olarak tuşları ile öğrenci kimlikleri dizileri içerecektir.