PHP | MYSQL:单个人的2个或更多列

时间:2016-02-24 07:58:08

标签: php mysql

我正在努力学习如何实现这张桌子。 我希望另一批记录能够制作另一列,如下图所示

enter image description here

这是我的疑问:

    SELECT 
          stud_name, 
          equivalent 
    FROM tb_student_record 
    WHERE instructor_id = 'INST-20131296' 
          AND criteria = 'Quiz' 
          AND description = 'BIOETHICS' 
          AND subj_code = 'N049' 
          AND term = 'Midterm';

任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:0)

更新了查询:

SELECT 
    stud_name, 
    GROUP_CONCAT(equivalent SEPARATOR '#') as equivalent
FROM tb_student_record 
WHERE instructor_id = 'INST-20131296' 
    AND criteria = 'Quiz' 
    AND description = 'BIOETHICS' 
    AND subj_code = 'N049' 
    AND term = 'Midterm'
GROUP BY `stud_name`;

在php中获取结果并使用explode函数分开。 该功能将起作用:

  

$ equivalent = explode(“#”,$ yourFetchArr->等效;)//依赖于   你的提取,我使用mysql_fetch_object();

现在$equivalent[0]$equivalent[1]是两个独立的等价物;

答案 1 :(得分:-2)

你可以做一个子查询应该看起来像这样

SELECT 
          stud_name, 
          equivalent,
          (SELECT 
               equivalent 
              FROM tb_student_record 
              WHERE instructor_id = 'INST-20131296' 
                AND criteria = 'Quiz' 
                AND description = 'BIOETHICS' 
                AND subj_code = 'N049' 
                AND term = 'Midterm';) AS equivalent2
    FROM tb_student_record 
    WHERE instructor_id = 'INST-20131296' 
          AND criteria = 'Quiz' 
          AND description = 'BIOETHICS' 
          AND subj_code = 'N049' 
          AND term = 'Midterm';

然后只需更改内部查询以选择查询的详细信息。如果您给我更多详细信息,我可以更新我的代码以更正确地反映内部查询