逗号分隔值到数组中

时间:2013-07-29 19:57:15

标签: php mysql database

我正在为学生创建一个数据库。

我创建了一个表“主题”

ID    NAME
1     Maths
2     Science
3     Biology
4     Social Studies

我创建了另一个表“学生”

ID    Name         Subject
1     Ram          1,2,3
2     Shyam        1,3,4
3     Hanuman      1,2,3,4

我想将学生的名字检索到php页面并显示他们的主题,我该怎么做:

Name          Subject
Ram           Maths, Science, Biology
Shyam         Maths, Biology, Social Studies 

我是PHP,MySQL的新手。

4 个答案:

答案 0 :(得分:4)

  1. 起点是从学生表中删除主题
  2. 并创建一个名为 student-subject
  3. 的“pivot-table

    例如:

    +------------+------------+
    | student_id | subject_id |
    +------------+------------+
    | 1          | 1          |
    | 1          | 2          |
    | 1          | 3          |
    | 2          | 1          |
    +------------+------------+
    

答案 1 :(得分:1)

与其他指出的一样,您的架构实际上并不是最优的,但您可以通过此查询实现您想要做的事情:

select st.Name, group_concat(su.name ORDER BY su.ID)
from Students st
inner join Subjects su on concat(',',st.Subject,',') like concat('%,',su.ID,',%')
group by st.Name

请参阅SQLFIDDLE:http://sqlfiddle.com/#!2/79286d/8/0

答案 2 :(得分:1)

以下代码适用于您现在拥有的数据库结构,位于更好的解决方案之下 php中最快的方法是尽可能少地选择信息。您可以加入表,但加入很慢,所以我们尝试其他方法。您可以为每个课程选择主题的名称,这对于每个查询的数据库来说不那么重,但是您正在进行大量的小查询,所以您不喜欢这样。

我建议的方法选择所有主题并将它们存储在一个数组中。通过将密钥设置为与主题的ID相同,您可以通过该密钥进一步访问信息,这非常快。

// Select all subjects:
$qSubj = "SELECT id,name FROM Subjects"; 
$sSubj = mysql_query($qSubj) or die(mysql_error()); // note: die() isnt pretty, and mysql_ should be mysqli_
// Set the variable which is going to store our information:
$subjects = array();
// For each row found, add a line:
while($fSubj = mysql_fetch_assoc($sSubj)){
    $subjects[ $fSubj['id'] ] = $fSubj; // here we save it, $subjects[2] will have the information of 'Science'
}

// Now we have the subjects, we can continue to the larger table:
$qStudent = "SELECT id,name,subjects FROM Students";
$sStudent = mysql_query($qStudent ) or die(mysql_error()); // note: die() isnt pretty, and mysql_ should be mysqli_
// Now we have selected the users, loop through them:
while($fStudent = mysql_fetch_assoc($sStudent )){
    // Here you can do whatever you want :) Im not a fan of echoing in this stage of the code
    // I prefer storing everything in something like $template, and output it at the end
    echo $fStudent['name'].' has following subjects: ';
    $courses = explode(",", $fStudent['subjects']); // By exploding on the comma, youhave them seperatly:
    foreach($courses as $k =>$subject_id){ // for each subject, get the name
        echo $subjects[ $subject_id ]['name'].' '; // Here we can use the information we stored earlier
    }
}

更好的解决方案:
你现在在while()中有一个foreach()来获取用户。如果你有几行,这没什么大不了的,但是如果要显示100多行,这个foreach循环会让它变慢。更好的方法是添加另一个表,链接它们:

主题:id,名称
学生:身份证,姓名
student_subject:student_id,subject_id。

最后一个表与student.subjects列大致相同,但这是在数据库中,而是在php中的数组中。 PHP和循环不是最好的朋友,您应该在student_subject的表中添加连接,并将学生的while()中的foreach()替换为查询,以选择该学生的所有subject_id。

答案 3 :(得分:-2)

您还可以检索主题列,将结果分解为数组,然后通过foreach语句查找每个主题的名称。

$subjects = array(1,2,3);

foreach ($subjects as $subject){
    $result = $db->query("SELECT `name` FROM `Subjects` WHERE `ID` = $subject");
         while($row = $result->fetch_assoc()) {
            echo $row['name'];
    }
}