2014-10-20 58 views
0

我有表tb_satkerPHP和MySQL加入2数据表

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

也表tb_upload

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

我想创建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 

这是我的php代码,这是创建主表,在此代码link

但如何使活动的映像表显示,如果没有值(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> 
+0

更新我的问题 – PNBP 2014-10-21 03:19:15

+0

是,创建主表,但条件语句尚未表(积极的,如果存在的价值和非活动如果没有值) – PNBP 2014-10-21 03:34:47

回答

0

您可以使用此查询:

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 
+0

我已经更新我的问题,请检查 – PNBP 2014-10-21 03:18:54