 |
ขอความช่วยเหลือเกี่ยวกับ SQL code ให้แสดงผลลัพธ์เป็นตารางเหมือนในรูปครับ เขียนยังไงครับ |
|
 |
|
|
 |
 |
|
Code (SQL)
select
product,
sum(holding='< 1 week') as `< 1 week`,
sum(holding='> 1 week') as `> 1 week`,
sum(holding='> 5 weeks') as `> 5 weeks`,
...
from table
group by product
|
 |
 |
 |
 |
Date :
2017-11-01 23:54:44 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ code ที่ให้มารันได้แล้วครับ
จาก code ค่า holding จะถูกกำหนดตายตัว
แต่ค่า holding จริงๆ จะเปลี่ยนแปลงตลอดขึ้นอยู่กับเวลาครับ
เช่น holding > 5 weeks อีกสามวันก็จะกลายเป็น > 6 weeks
จะต้องเขียน code ยังไงครับ
|
 |
 |
 |
 |
Date :
2017-11-02 09:45:11 |
By :
Siripong-Promduang |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
และค่าใน database เปลี่ยนแปลงยังไง ใช้อะไรเปลี่ยน
|
 |
 |
 |
 |
Date :
2017-11-02 11:47:51 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ข้อมูล database เป็นแบบนี้ครับ

php code ที่ผมใช้ update ครับ
<?
$sql_data="SELECT * FROM Inventory ORDER BY last_update DESC";
$result_data = mysql_query($sql_data);
while($result=mysql_fetch_array($result_data))
{
$mid = $result[mid];
$fdate = $result[Input_time];
$cdate = date("Y-m-d");
//calculate qty week holding
$timediffwk = (strtotime($cdate)-strtotime($fdate))/604800; // 1 week = 60*60*24*7
$timediffwk1 =floor($timediffwk);
//update holding time
$sql_holding = "Update ProductHDD set hold='$timediffwk1' where mid='$mid'";
$result_update = mysql_query($sql_holding);
}
?>
ผลลัพธ์ที่ใช้ code พี่เขียนออกมาได้แบบนี้ครับ
<?
$sql_hold= "select product, hold,
sum(hold='0') as `<1week`,
sum(hold='1') as `>1week`,
sum(hold='2') as `>2weeks`,
sum(hold='3') as `>3weeks`,
sum(hold='4') as `>4weeks`,
sum(hold='5') as `>5weeks`,
sum(hold='6') as `>6weeks`,
sum(hold='7') as `>7weeks`,
sum(hold='8') as `>8weeks`,
sum(hold='9') as `>9weeks`,
sum(hold='10') as `>10weeks`,
COUNT(product) as `Total`
from Inventory group by product ORDER BY Total DESC";
$result_hold = mysql_query($sql_hold);
$i=0;
?>
<table width="99%" border="1" cellpadding="0" cellspacing="0" bordercolor="#CCCCCC">
<tr>
<td rowspan="2" align="center" bgcolor="#99FFFF"><strong>No.</strong></td>
<td rowspan="2" align="center" bgcolor="#99FFFF"><strong>Product</strong></td>
<td colspan="5" align="center" bgcolor="#99FFFF"><strong>HDD Holding in PE Lap </strong></td>
<td rowspan="2" align="center" bgcolor="#99FFFF"><strong>Total<br/>(HDD)
</strong></td>
</tr>
<tr>
<td align="center" bgcolor="#99FFFF"><strong>< 1 Wk</strong></td>
<td align="center" bgcolor="#99FFFF"><strong>> 1 Wk</strong></td>
<td align="center" bgcolor="#99FFFF"><strong>> 2 Wk</strong></td>
<td align="center" bgcolor="#99FFFF"><strong>> 3 Wk</strong></td>
<td align="center" bgcolor="#99FFFF"><strong>> 4 Wk</strong></td>
</tr>
<? while($result_week=mysql_fetch_array($result_hold)) { $i++; ?>
<tr>
<td align="center"><? echo $i;?></td>
<td align="center"><strong><? echo $product = $result_week[product];?></strong></td>
<td align="center"><? echo $result_week['<1week'];?></td>
<td align="center"><? echo $result_week['>1week'];?></td>
<td align="center"><? echo $result_week['>2weeks'];?></td>
<td align="center"><? echo $result_week['>3weeks'];?></td>
<td align="center"><? echo $result_week['>4weeks'];?></td>
<td align="center"><? echo $result_week['Total'];?></td>
</tr>
<? } ?>
</table>

|
 |
 |
 |
 |
Date :
2017-11-02 14:11:48 |
By :
Siripong-Promduang |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมจะ aoturun เพื่อ calculation week และ update "Input_time" ทุกวันเวลาเที่ยงคืนครับ
|
 |
 |
 |
 |
Date :
2017-11-02 14:15:03 |
By :
Siripong-Promduang |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
หาค่า hold มาสร้าง label
Code (PHP)
$rs=$db->query('select distinct hold from tablename order by hold');
$sql = "select hold";
while($ro=$rs->fetch_assoc()){
$h = $ro['hold'];
$sql.=', sum(hold='.$h.') as '.($h==0 ? '< 1 week' : '`> '.$h.' week'.($h>1? 's`' : '`');
}
$sql.=' from tablename group by product';
|
 |
 |
 |
 |
Date :
2017-11-02 14:44:24 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตกวงเล็บปิดไปตัว บันทัด 5
code ที่ให้เป็นแค่ตัวอย่างบางส่วน ต้องนำไปเขียนเพิ่มเอาเองนะครับ
|
ประวัติการแก้ไข 2017-11-02 18:00:06
 |
 |
 |
 |
Date :
2017-11-02 17:56:35 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|