如何比较2个表中每行的2个值?

时间:2016-03-09 13:46:16

标签: php mysql sql

如何比较2个表中的2个值?

我使用2 while(嵌套while)但是表格逐个检查,第二个表格中的结果来自第一个表格中的每一行。 如何打印一次结果?

我想要这样的结果 result's image

这是我目前的代码

<?php               
                    $query1 = mysql_query("SELECT * FROM riwayat_training_detail AS s JOIN materi AS t JOIN riwayat_training AS u JOIN karyawn AS v WHERE v.id_karyawan = $id1 AND s.nik LIKE v.nik AND u.subject no LIKE t.subject no AND s.id_riwayat_training LIKE u.id_riwayat_training ");
                    $i=1;
                    while($row1 = mysql_fetch_array($query1))
                    {
                        $date = $row1['date'];
                        $subject1 = $row1['subject name'];
                ?>
            <table class="table table-bordered">
                <tr>
                    Training yang diikuti
                    <td class="table-bordered">No</td>
                    <td class="table-bordered">date</td>
                    <td class="table-bordered">subject date</td>
                    <td class="table-bordered">No SOP</td>
                    <td class="table-bordered">revision no</td>
                    <td class="table-bordered">Trainer</td>
                    <td class="table-bordered">institution</td>
                </tr>

                <tr>
                    <td class="table-bordered"><?php echo $i; ?></td>
                    <td class="table-bordered"><?php echo date("j/F/Y", strtotime($date)); ?></td>
                    <td class="table-bordered"><?php echo $subject1; ?></td>
                    <td class="table-bordered"><?php echo $row1['no_sop']; ?></td>
                    <td class="table-bordered"><?php echo $row1['revision_no']; ?></td>
                    <td class="table-bordered"><?php echo $row1['trainer']; ?></td>
                    <td class="table-bordered"><?php echo $row1['institution']; ?></td> 
                </tr>
            </br>
            <table class="table table-bordered">
                <tr>
                    Training yang belum diikuti
                    <td class="table-bordered">No</td>
                    <td class="table-bordered">subject no</td>
                    <td class="table-bordered">subject date</td>
                    <td class="table-bordered">No SOP</td>
                </tr>
                <?php
                    $query2 = mysql_query("SELECT * FROM header_jabatan AS r JOIN materi AS q JOIN karyawn AS p WHERE q.subject no LIKE r.id_header AND r.id_jabatan LIKE  p.id_jabatan AND p.id_karyawan = $id1 ORDER BY q.subject no ASC");
                    $x=1;
                    while($row2 = mysql_fetch_array($query2))
                    {
                        $subject2 = $row2['subject name'];
                        if (strcasecmp($materi1, $materi2) != 0)
                        {
                ?>
                <tr>
                    <td class="table-bordered"><?php echo $i; ?></td>
                    <td class="table-bordered"><?php echo $row2['subject no'] ?></td>
                    <td class="table-bordered"><?php echo $subject22; ?></td>
                    <td class="table-bordered"><?php echo $row2['no_sop']; ?></td>
                </tr>     
                <?php  
                        }
                    $x++;       
                    }
                    $i++;
                }

                ?>
            </table>
        </table>

请帮助我该怎么办?

这是我同一主题的第二个问题,我真的需要帮助我坚持这个..

3 个答案:

答案 0 :(得分:0)

您可以使用 CASE 声明检查此链接以获取更多有关如何使用它的详细信息。

check this link

答案 1 :(得分:0)

这是否符合您的要求?

UPDATE table2 SET Status = "YES" WHERE SubjectName IN (SELECT SubjectName FROM table1)

或者您是否希望它也使用修订版?您的图片仅涉及SubjectName。

答案 2 :(得分:0)

对于上面结果图片的情况,最好使用 LEFT JOIN RIGHT JOIN

见下面的例子:

> CREATE TABLE table1 (subject VARCHAR(10), value INT);
> CREATE TABLE table2 (id INT, subject VARCHAR(10));

> INSERT INTO table1 VALUES ('XXX', 23),('ZZZ', 24);
> INSERT INTO table2 VALUES (1, 'XXX'),(2, 'YYY'), (3, 'ZZZ');

> SELECT * FROM table1;
+---------+-------+
| subject | value |
+---------+-------+
| XXX     |    23 |
| ZZZ     |    24 |
+---------+-------+

> SELECT * FROM table2;
+------+---------+
| id   | subject |
+------+---------+
|    1 | XXX     |
|    2 | YYY     |
|    3 | ZZZ     |
+------+---------+
> SELECT a.*, IF(b.value IS NULL, 'No', 'Yes') as status FROM table2 as a LEFT JOIN table1 as b ON b.subject = a.subject;
+------+---------+--------+
| id   | subject | status |
+------+---------+--------+
|    1 | XXX     | Yes    |
|    3 | ZZZ     | Yes    |
|    2 | YYY     | No     |
+------+---------+--------+