简体   繁体   English

PHP 和 MySQL 加入 2 个数据表

[英]PHP and MySQL To Join 2 Data Table

i have table tb_satker我有表 tb_satker

kode_propinsi | nama_satker  |  kode_satker
500           | A            |  1
500           | B            |  2
500           | C            |  3
500           | D            |  4

also table tb_upload还有表 tb_upload

kode_propinsi | kode_satker  |  month
500           | A            |  1
500           | A            |  2
500           | B            |  3

I want to create php and mysql code to generate table like this我想创建php和mysql代码来生成这样的表

No | UPT | Jan | Feb | Mar | Apr | Mei | Jun | Jul | Ags | Sep | Okt | Nov | Des
1  | A   | 1   | 2   | 0   | 0   | 0   | 0   | 0   |  0  | 0   | 0   | 0   | 0
2  | B   | 0   | 0   | 3   | 0   | 0   | 0   | 0   |  0  | 0   | 0   | 0   | 0
3  | C   | 0   | 0   | 0   | 0   | 0   | 0   | 0   |  0  | 0   | 0   | 0   | 0
4  | D   | 0   | 0   | 0   | 0   | 0   | 0   | 0   |  0  | 0   | 0   | 0   | 0

This is my php code, it's work to create master table, live code at this link这是我的 php 代码,可以在此链接上创建主表和实时代码

But how to make table with inactive images to show if no value (0) and active images if month filled with value (1/2/3/4... etc)但是如何制作带有非活动图像的表格以显示如果没有值(0)和活动图像,如果月份填充了值(1/2/3/4 ...等)

<table class="table table-hover">
<thead>
    <tr>
        <th>No</th>
        <th>UPT</th>
        <th>Jan</th>
        <th>Feb</th>
        <th>Mar</th>
        <th>Apr</th>
        <th>Mei</th>
        <th>Jun</th>
        <th>Jul</th>
        <th>Ags</th>
        <th>Sep</th>
        <th>Okt</th>
        <th>Nov</th>
        <th>Des</th>
    </tr>
</thead>
<tbody>
<?
$view = "SELECT * FROM $tb_satker WHERE kode_propinsi='$propinsi'";
$result = mysql_query($view);
$jumlah=mysql_num_rows($result);
$nomor=0;
while
($baris=mysql_fetch_array($result))
{
$namasatker=$baris['nama_satker'];
$kodesatker=$baris['kode_satker'];
$kodepropinsi=$baris['kode_propinsi'];
$nomor=$nomor+1;
?>
    <tr>
        <th><? echo $nomor;?></th>
        <th><? echo $namasatker;?></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
        <th><img src="images/inactive.png"></th>
<? }?>
    </tr>
</tbody>
</table>

You can use this query:您可以使用此查询:

SELECT a.id,a.code,a.name,
max(case when b.month=1 then 1 else "-" end) as month1,
max(case when b.month=2 then 1 else "-" end) as month2,
max(case when b.month=3 then 1 else "-" end) as month3
FROM `table_a` as a
left join table_b as b on a.name= b.name
group by a.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM