我有两个主表。因为关系是多对多的,所以我决定创建链接表来创建一对多的关系。
这是主表:

第一桌

MariaDB [ittresnamuda]> SELECT * FROM tb_item_request;
+---------+-----------+
| ID_TIPE | NAMA_TIPE |
+---------+-----------+
|       1 | Login     |
|       2 | Printer   |
|       3 | Monitor   |
|       4 | Computer  |
|       5 | Network   |
|       6 | Lain-lain |
+---------+-----------+
6 rows in set (0.00 sec)


第二表

MariaDB [ittresnamuda]> select a.ID_REQUEST, a.CATATAN from tb_requestfix a;
+------------+---------------------------------+
| ID_REQUEST | CATATAN                         |
+------------+---------------------------------+
|          3 | Akan kami cek jaringan tersebut |
|          4 | Iya, go ahead. Appproved        |
|          5 | Sudah di refill                 |
|         28 | Saja                            |
+------------+---------------------------------+


设置4行(0.00秒)

链接表

MariaDB [ittresnamuda]> select * from tb_link_item_request;
+----+------------+---------+
| ID | id_request | id_item |
+----+------------+---------+
| 16 |          4 |       1 |
| 17 |          4 |       2 |
| 18 |          4 |       3 |
| 19 |          4 |       4 |
| 20 |          4 |       5 |
| 21 |          4 |       6 |
| 26 |          3 |       2 |
| 27 |          3 |       3 |
| 28 |          3 |       4 |
| 29 |          3 |       5 |
| 30 |          5 |       6 |
| 56 |         28 |       2 |
+----+------------+---------+
12 rows in set (0.00 sec)


我需要选择这种格式的表:

+------------+---------------------------------+-----------------------+
| ID_REQUEST | CATATAN                         | L | P | M | C | N | LL|
+------------+---------------------------------+-----------------------+
|          3 | Akan kami cek jaringan tersebut |   | 1 | 1 | 1 | 1 | 1 |
|          4 | Iya, go ahead. Appproved        | 1 | 1 | 1 | 1 | 1 | 1 |
|          5 | Sudah di refill                 |   |   | 1 |   |   | 1 |
|         28 | Saja                            |   | 1 |   |   |   |   |
+------------+---------------------------------+-----------------------+

L = Login, P = Printer, M = Monitor, so on, so on


如您所见,在ID_REQUEST = 4上,在tb_link_item_request上有6条记录。因此,我需要的表上必须有一个值= 1。我想在查询,联接,组上使用的是我想的东西吗?

任何帮助,它表示赞赏。

更新,正如下面的评论,似乎不可能创建这样的?
这样的回合:

+------------+---------------------------------+-----------------------+
| ID_REQUEST | CATATAN                         | L | P | M | C | N | LL|
+------------+---------------------------------+-----------------------+
|          3 | Akan kami cek jaringan tersebut |   | 2 | 3 | 4 | 5 | 6 |
|          4 | Iya, go ahead. Appproved        | 1 | 2 | 3 | 4 | 5 | 6 |
|          5 | Sudah di refill                 |   |   | 3 |   |   | 6 |
|         28 | Saja                            |   | 2 |   |   |   |   |
+------------+---------------------------------+-----------------------+

L = Login, P = Printer, M = Monitor, so on, so on


可能吗 ?

最佳答案

请为其他两个表的LINK表中的列命名相同的名称,即tb_link_item_request(ID,ID_REQUEST,ID_TIPE),然后使用自然连接如下:

SELECT ID_REQUEST,CATATAN, ID_TIPE
FROM tb_item_request
     natural join tb_link_item_request
     natural join tb_requestfix;


现在使用PHP将您的数据显示为

$query="SELECT ID_REQUEST,CATATAN, ID_TIPE
        FROM tb_item_request
        natural join tb_link_item_request
        natural join tb_requestfix order by ID_REQUEST,ID_TIPE";
$result=mysqli_query($link,$query);
if (!$result)
{
  echo("Error description: " . mysqli_error($link));
}
if(mysqli_num_rows($result)>0){
 echo '<table>'
  .'<tr>'
   .'<th>ID_REQUEST</th>'
   .'<th>CATATAN</th>'
   .'<th>L</th>'
   .'<th>P</th>'
   .'<th>M</th>'
   .'<th>C</th>'
   .'<th>N</th>'
   .'<th>LL</th>'
  .'</tr>';

  $prevIDReq=null;
  $rowCount=0;
  while($row=mysqli_fetch_array($result)){

    if($prevIDReq!=$row['ID_REQUEST']){
      if($prevIDReq!=null)
      {
         while($rowCount<=6){
           echo '<td></td>';
           $rowCount++
         }
         echo '</tr>';
         $rowCount=0;
      }

      echo "<tr>"
           ."<td>".$row['ID_REQUEST']."</td>"
           ."<td>".$row['CATANTAN']."</td>";

    }

    if($row['ID_TIPE']==1){
      echo '<td>1</td>';
    }

    if($row['ID_TIPE']==2){
      while($rowCount<1){
         echo '<td></td>';
         $rowCount++;
      }
      echo '<td>1</td>';
    }

    if($row['ID_TIPE']==3){
      while($rowCount<2){
         echo '<td></td>';
         $rowCount++;
      }
      echo '<td>1</td>';
    }

    if($row['ID_TIPE']==4){
      while($rowCount<3){
         echo '<td></td>';
         $rowCount++;
      }
      echo '<td>1</td>';
    }

    if($row['ID_TIPE']==5){
      while($rowCount<4){
         echo '<td></td>';
         $rowCount++;
      }
      echo '<td>1</td>';
    }

    if($row['ID_TIPE']==6){
      while($rowCount<5){
         echo '<td></td>';
         $rowCount++;
      }
      echo '<td>1</td>';
    }

    $rowCount++:
  }


  while($rowCount<=6){
           echo '<td></td>';
           $rowCount++
         }

  echo '</tr>'
       .'</table>';
}


更新:根据更新的问题,您可以在上面的代码中使用"<td>".$row['ID_TIPE']."</td>"代替'<td>1</td>'

关于mysql - 在选择查询中通过联接主表和链接表为列提供值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36245661/

10-10 04:49