通过在选择查询上连接主表和链接表为列提供值

时间:2016-03-27 09:08:00

标签: 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)

SECOND TABLE

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秒)

LINK TABLE

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 = 4tb_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

有可能吗?

2 个答案:

答案 0 :(得分:0)

请同样给LINK表中的列w.r.t其他两个表,即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>'

答案 1 :(得分:0)

您可以使用条件聚合来创建数据透视结果:

@WebMethod(operationName = "GetOrdersStatuses", action = "https://www.foo.com/atom_api/call/atom_api&method=GetOrdersStatuses")
@WebResult(name = "GetOrdersStatusesReturn", partName = "GetOrdersStatusesReturn")
public String getOrdersStatuses(
    @WebParam(name = "authenticate", partName = "authenticate")
    Auth authenticate);

结果:

SELECT rf.ID_REQUEST, rf.CATATAN
    , BIT_OR(lir.id_item <=> 1) OR NULL AS Login
    , BIT_OR(lir.id_item <=> 2) OR NULL AS Printer
    -- and so on
FROM tb_requestfix rf
LEFT JOIN tb_link_item_request lir ON lir.id_request = rf.ID_REQUEST
GROUP BY rf.ID_REQUEST, rf.CATATAN

sqlfiddle

使用| ID_REQUEST | CATATAN | Login | Printer | |------------|---------------------------------|--------|---------| | 3 | Akan kami cek jaringan tersebut | (null) | 1 | | 4 | Iya, go ahead. Appproved | 1 | 1 | | 5 | Sudah di refill | (null) | (null) | | 28 | Saja | (null) | 1 | 表中的数据,用PHP构建该查询。

您可以使用tb_item_requestSUM()BIT_OR()或最符合您要求的内容。