2012-05-12 67 views
2

假設我有一張名爲students的表格,其中包含所有學生的列表。每個學生可以將許多課程存儲在另一個表中,由學生ID標識。是否有可能在mysql中執行此查詢?

我想要做一個查詢,它將從學生表中獲取包含學生信息的行,並在一個查詢中獲取他所有的課程。我想輸出是這樣的:

Array 
(
    [0] => Array 
     (
      [id] => 5 
      [firstName] => Bob 
      [lastName] => Smith 
      [email] => .... 
      [courses] => Array 
       (
        [0] => Array 
         (
          [id] => 30 
          [name] => Test Course 
          [price] => 400 
         ) 

        [1] => Array 
         (
          [id] => 30 
          [name] => Test Course 2 
          [price] => 300 
         ) 

       ) 

     ) 

) 

讓學生的信息,我會做這個查詢:

SELECT * FROM students WHERE something ='something' 

要獲得這些課程的學生,我會做這樣的:

SELECT * FROM courses WHERE studentId = '5' 

是否有可能一次執行兩個查詢並獲得類似於上述數組的輸出,還是必須遍歷每個學生並獲取其課程,然後將其附加到數組?

+0

我想'group_concat'可能的幫助。鏈接:http://www.dougboude.com/blog/1/2009/12/A-SWEET-Little-MySQL-Function-GroupConcat.cfm – Strawberry

+0

你在尋找SQL語法還是爲了進入你的數組格式? –

+0

http://stackoverflow.com/a/5378180/170365 – Strawberry

回答

1
$query = "select * from students inner join courses on studentID = '5' where something = 'something'"; 
$result = mysql_db_query ($dbname, $query, $link); 
$id = $row[0]; # corresponding to row in students database 
$firstName= $row[1]; # corresponding to row in students database 
$lastName = $row[2]; # corresponding to row in students database 
$email = $row[3]; # corresponding to row in students database 
while($row = mysql_fetch_array($result)) { 
    echo 'id: '.$row[id]; 
    echo '<br>'; 
    echo 'name: '.$row[name]; 
    echo '<br>'; 
    echo 'price: '.$row[price]; 
    echo '<br>'; 
} 
+0

是否有可能對studentId = students.id'做內部連接課程,而不是將學生代碼硬編碼爲5?我想讓所有的學生,而不是一次一個。 –

+0

@Click Yes只是用''取代'5'。$ studentId。''' – Rodney

+0

不,我一次只能給一個學生提供,如果我有100個學生,我將不得不運行這個查詢100他們所有人的時間 –

1

您可以通過連接表一起做到這一點:

select s.*, c.* 
from students s join 
    courses c 
    on s.studentid = c.studentid 
where s.something = 'something' 

如果你想在一排學生信息,並在另一個過程中,那麼它是不可能的。所有行必須具有相同的列。

+0

我想讓課程信息成爲學生排上的數組,那不可能呢? –

+0

這不會創建一個多維數組。 – Strawberry

+0

@Strawberry它不可能做一個多維數組,然後除了循環? –

1

基本上新代碼將是:

$query = "select * from students"; 
$result = mysql_db_query ($dbname, $query, $link); 
while($row = mysql_fetch_array($result)) { 
    $id = $row[0]; # corresponding to row in students database 
    $firstName= $row[1]; # corresponding to row in students database 
    $lastName = $row[2]; # corresponding to row in students database 
    $email = $row[3]; # corresponding to row in students database 
    echo 'id: '.$id; 
    echo '<br>'; 
    echo 'firstName: '.$firstName; 
    echo ' '; 
    echo 'lastName: '.$lastName; 
    echo '<br>'; 
    echo 'email: '.$email; 
    echo '<br>'; 
    $query = "select * from courses where id = '".$id."'"; 
    $result2 = mysql_db_query ($dbname, $query, $link); 
    while($row2 = mysql_fetch_array($result2)) { 
    echo 'name: '.$row2[1]; # corresponding to row in courses database 
    echo '<br>'; 
    echo 'price: '.$row2[2]; # corresponding to row in courses database 
    echo '<br>'; 
    } 
} 
相關問題