mysql - 为选择查询中加入主表和链接表的列赋值

标签 mysql pivot

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

第一张表

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 条记录。所以,我需要的表上必须有一个 value = 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/

相关文章:

pandas - ValueError : Index contains duplicate entries, 无法 reshape

sql - 甲骨文 : Dynamic column name

mysql - SO 对动态旋转原因的回答 'Error in SQL Synthax'

没有存储例程的MySQL循环

mysql - 有人可以告诉我 slugging percentage 查询有什么问题吗?

c# - 如何连接.Net Webservice 与本地数据库

pandas - 如何使用计数在 Pandas 中进行 vlookup

mysql - 无法加载文件或程序集“mysql.data,版本=6.9.5.0”

mysql - 按 id 分组并合并数据(其中为 null)

postgresql - 数据透视表/交叉表 : I wan to return dynamic columns