根据一个列ID(标题

时间:2019-04-02 09:23:06

标签: php html mysql

    Table1 name : **class_subjects**
     +----+---------+-------+
    | ID | class_ID| Paper |
    +----+---------+-------+
    |  1 |       2 | Sub1  |
    |  2 |       7 | Sub3  |
    |  3 |       2 | Sub5  |
    |  4 |       2 | Sub2  |
    |  5 |       4 | Sub1  |
    |  6 |       7 | Sub5  |
    |  7 |       5 | Sub3  |
    |  8 |       5 | Sub2  |
    |  9 |       3 | Sub1  |
    | 10 |       2 | Sub1  |
    | 11 |       1 | Sub9  |
    +----+---------+-------+
     Table2 name : **classes**
    +----+--------------+
    | ID |  className   |
    +----+--------------+
    |  1 | Multimedia   |
    |  2 | Animation    |
    |  3 | Visual EFX   |
    |  4 | Visual Comm  |
    |  5 | Visual Arts  |
    |  6 | BCA          |
    |  7 | BSc          |
    |  8 | BA           |
    |  9 | BCom         |
    | 10 | BFT          |
    +----+--------------+
    I do have two tables with the names 'sjcc_class_subjects' and 'sjcc_classes'. 

在第一个表列“ ClassID ”中表示第二个表“ ID ”。          像//

+-----------+---------+
    | **Animation** |     |
    +-----------+---------+
    | #         | Subject |
    | 1         | Sub1    |
    | 2         | Sub5    |
    | 3         | Sub2    |
    | 4         | Sub1    |
    +-----------+---------+
    +-----+---------+
    | **BSc** |     |
    +-----+---------+
    | #   | Subject |
    | 1   | Sub3    |
    | 2   | Sub5    |
    +-----+---------+
    +-------------+---------+
    | Visual Comm |         |
    +-------------+---------+
    | #           | Subject |
    | 1           | Sub1    |
    +-------------+---------+

我正在使用以下代码,但是输出显示不正确。请帮助我解决这个问题。提前致谢。 (对不起,编码很差。)

   <?php
    $sjcc="SELECT classes.ID, classes.className, class_subjects.class_ID, class_subjects.paper FROM classes, class_subjects group by class_subjects.class_ID";      
    $sjcc_result    = $conn->query($sjcc);
    while($sjcc_result_fetch    = $sjcc_result->fetch_assoc()) {
    ?>
        <li>                
         <h1>BA MM <?php echo $sjcc_result_fetch['className'];?></h1>
          <table class="table">
          <thead>
          <tr>
           <th>#</th>
           <th>Subject</th>
         </tr>
         </thead>
        <?php
        while($sjcc_result_fetch2    = $sjcc_subject_result->fetch_assoc()) 
       {
        ?>
        <tbody>
         <tr>
           <td>1</td>
           <td><?php echo $sjcc_result_fetch2    ['paper_name']; ?></td>
         </tr>
         </tbody>
        <?php } ?>
        </table>
     </li>
        <?php } ?> 

请帮助我解决此问题,找不到任何解决方案。如果有的话,对我来说会很棒

3 个答案:

答案 0 :(得分:3)

您只需要在SQL查询中添加 ORDER BY

SQL中的ORDER BY语句用于根据一个或多个列对获取的数据进行升序或降序排序。

默认情况下,ORDER BY以升序对数据进行排序。 我们可以使用关键字DESC对数据进行降序排序,并使用关键字ASC对数据进行升序排序。

查询-

SELECT CLASSES.id,
       CLASSES.classname,
       CLASS_SUBJECTS.class_id,
       CLASS_SUBJECTS.paper
FROM   CLASSES,
       CLASS_SUBJECTS
GROUP  BY CLASS_SUBJECTS.class_id 
ORDER BY CLASS_SUBJECTS.class_id ASC;

答案 1 :(得分:0)

对于订单,您应该使用ORDER BY而不是

    SELECT classes.ID
    , classes.className
    , class_subjects.class_ID
    , class_subjects.paper 
    FROM classes
    , class_subjects 
    ORDER  by class_subjects.class_ID

您应该使用显式联接,在这种情况下,请使用CROSS JOIN 如果您想获得独特的结果,请使用DISTINCT子句,而不要使用分组方式

    SELECT distinct  classes.ID
    , classes.className
    , class_subjects.class_ID
    , class_subjects.paper 
    FROM classes
    CROSS JOIN  class_subjects 
    ORDER  by class_subjects.class_ID

答案 2 :(得分:0)

使用连接从两个表中获取数据并按排序顺序进行排序

select nt.id,nt.Paper as Subject,ud.className  from class_subjects as nt join classes as ud on nt.class_ID =ud.ID order by ud.className    asc