所以我有这个SQL表 price_list 有
| ID | R_NO | DATE | CURRENCY | PRICE | STATUS |
------------------------------------------------------
| 1 | 9021 | 2016-01-01 | US | 300 | true |
| 2 | 9021 | 2016-01-02 | US | 300 | true |
| 3 | 9021 | 2016-01-03 | US | 500 | true |
| 4 | 9021 | 2016-01-04 | US | 600 | false |
| 5 | 9021 | 2016-01-05 | US | 600 | true |
我必须在指定月份(从第1天到最后一天。例如:1-31)获得 PRICE ,条件是如果房间号( R_NO )不可用( STATUS: true 或 false )然后它会显示为空或 '0'。 所以这是代码;
<?php
$base_date = "2016-01-01";
$sql_start = "Select ";
$sql_middle = " from price_list as t1";
$sql_end = " where t1.DATE = '2016-01-01' AND t1.R_NO = '9021'" ;
for($x = 1; $x <=31; $x++){
$kess = "(CASE WHEN t".($x).".STATUS = 'false' THEN t".($x).".PRICE = '' ELSE t".$x.".PRICE END)";
if($x == 31){
$sql_start .= "".$kess." as 'Jan ".$x."'";
}else{
$sql_start .= "".$kess." as 'Jan ".$x."',";
}
if($x < 31){
$sql_middle .= " left join price_list as t".($x + 1)." on t".$x.".R_NO = t".($x + 1).".R_NO and t".($x + 1).".DATE ='".date('Y-m-d', strtotime($base_date . '+'.$x.' day'))."'";
}
}
echo $sql_start.$sql_middle.$sql_end;
这将生成SQL代码(这是一个很长的代码)
Select (CASE WHEN t1.STATUS = 'false' THEN t1.PRICE = '' ELSE t1.PRICE END) as 'Jan 1',(CASE WHEN t2.STATUS = 'false' THEN t2.PRICE = '' ELSE t2.PRICE END) as 'Jan 2',(CASE WHEN t3.STATUS = 'false' THEN t3.PRICE = '' ELSE t3.PRICE END) as 'Jan 3',(CASE WHEN t4.STATUS = 'false' THEN t4.PRICE = '' ELSE t4.PRICE END) as 'Jan 4',(CASE WHEN t5.STATUS = 'false' THEN t5.PRICE = '' ELSE t5.PRICE END) as 'Jan 5',(CASE WHEN t6.STATUS = 'false' THEN t6.PRICE = '' ELSE t6.PRICE END) as 'Jan 6',(CASE WHEN t7.STATUS = 'false' THEN t7.PRICE = '' ELSE t7.PRICE END) as 'Jan 7',(CASE WHEN t8.STATUS = 'false' THEN t8.PRICE = '' ELSE t8.PRICE END) as 'Jan 8',(CASE WHEN t9.STATUS = 'false' THEN t9.PRICE = '' ELSE t9.PRICE END) as 'Jan 9',(CASE WHEN t10.STATUS = 'false' THEN t10.PRICE = '' ELSE t10.PRICE END) as 'Jan 10',(CASE WHEN t11.STATUS = 'false' THEN t11.PRICE = '' ELSE t11.PRICE END) as 'Jan 11',(CASE WHEN t12.STATUS = 'false' THEN t12.PRICE = '' ELSE t12.PRICE END) as 'Jan 12',(CASE WHEN t13.STATUS = 'false' THEN t13.PRICE = '' ELSE t13.PRICE END) as 'Jan 13',(CASE WHEN t14.STATUS = 'false' THEN t14.PRICE = '' ELSE t14.PRICE END) as 'Jan 14',(CASE WHEN t15.STATUS = 'false' THEN t15.PRICE = '' ELSE t15.PRICE END) as 'Jan 15',(CASE WHEN t16.STATUS = 'false' THEN t16.PRICE = '' ELSE t16.PRICE END) as 'Jan 16',(CASE WHEN t17.STATUS = 'false' THEN t17.PRICE = '' ELSE t17.PRICE END) as 'Jan 17',(CASE WHEN t18.STATUS = 'false' THEN t18.PRICE = '' ELSE t18.PRICE END) as 'Jan 18',(CASE WHEN t19.STATUS = 'false' THEN t19.PRICE = '' ELSE t19.PRICE END) as 'Jan 19',(CASE WHEN t20.STATUS = 'false' THEN t20.PRICE = '' ELSE t20.PRICE END) as 'Jan 20',(CASE WHEN t21.STATUS = 'false' THEN t21.PRICE = '' ELSE t21.PRICE END) as 'Jan 21',(CASE WHEN t22.STATUS = 'false' THEN t22.PRICE = '' ELSE t22.PRICE END) as 'Jan 22',(CASE WHEN t23.STATUS = 'false' THEN t23.PRICE = '' ELSE t23.PRICE END) as 'Jan 23',(CASE WHEN t24.STATUS = 'false' THEN t24.PRICE = '' ELSE t24.PRICE END) as 'Jan 24',(CASE WHEN t25.STATUS = 'false' THEN t25.PRICE = '' ELSE t25.PRICE END) as 'Jan 25',(CASE WHEN t26.STATUS = 'false' THEN t26.PRICE = '' ELSE t26.PRICE END) as 'Jan 26',(CASE WHEN t27.STATUS = 'false' THEN t27.PRICE = '' ELSE t27.PRICE END) as 'Jan 27',(CASE WHEN t28.STATUS = 'false' THEN t28.PRICE = '' ELSE t28.PRICE END) as 'Jan 28',(CASE WHEN t29.STATUS = 'false' THEN t29.PRICE = '' ELSE t29.PRICE END) as 'Jan 29',(CASE WHEN t30.STATUS = 'false' THEN t30.PRICE = '' ELSE t30.PRICE END) as 'Jan 30',(CASE WHEN t31.STATUS = 'false' THEN t31.PRICE = '' ELSE t31.PRICE END) as 'Jan 31' from price_list as t1 left join t_listing_price as t2 on t1.R_NO = t2.R_NO and t2.DATE ='2016-01-02' left join t_listing_price as t3 on t2.R_NO = t3.R_NO and t3.DATE ='2016-01-03' left join t_listing_price as t4 on t3.R_NO = t4.R_NO and t4.DATE ='2016-01-04' left join t_listing_price as t5 on t4.R_NO = t5.R_NO and t5.DATE ='2016-01-05' left join t_listing_price as t6 on t5.R_NO = t6.R_NO and t6.DATE ='2016-01-06' left join t_listing_price as t7 on t6.R_NO = t7.R_NO and t7.DATE ='2016-01-07' left join t_listing_price as t8 on t7.R_NO = t8.R_NO and t8.DATE ='2016-01-08' left join t_listing_price as t9 on t8.R_NO = t9.R_NO and t9.DATE ='2016-01-09' left join t_listing_price as t10 on t9.R_NO = t10.R_NO and t10.DATE ='2016-01-10' left join t_listing_price as t11 on t10.R_NO = t11.R_NO and t11.DATE ='2016-01-11' left join t_listing_price as t12 on t11.R_NO = t12.R_NO and t12.DATE ='2016-01-12' left join t_listing_price as t13 on t12.R_NO = t13.R_NO and t13.DATE ='2016-01-13' left join t_listing_price as t14 on t13.R_NO = t14.R_NO and t14.DATE ='2016-01-14' left join t_listing_price as t15 on t14.R_NO = t15.R_NO and t15.DATE ='2016-01-15' left join t_listing_price as t16 on t15.R_NO = t16.R_NO and t16.DATE ='2016-01-16' left join t_listing_price as t17 on t16.R_NO = t17.R_NO and t17.DATE ='2016-01-17' left join t_listing_price as t18 on t17.R_NO = t18.R_NO and t18.DATE ='2016-01-18' left join t_listing_price as t19 on t18.R_NO = t19.R_NO and t19.DATE ='2016-01-19' left join t_listing_price as t20 on t19.R_NO = t20.R_NO and t20.DATE ='2016-01-20' left join t_listing_price as t21 on t20.R_NO = t21.R_NO and t21.DATE ='2016-01-21' left join t_listing_price as t22 on t21.R_NO = t22.R_NO and t22.DATE ='2016-01-22' left join t_listing_price as t23 on t22.R_NO = t23.R_NO and t23.DATE ='2016-01-23' left join t_listing_price as t24 on t23.R_NO = t24.R_NO and t24.DATE ='2016-01-24' left join t_listing_price as t25 on t24.R_NO = t25.R_NO and t25.DATE ='2016-01-25' left join t_listing_price as t26 on t25.R_NO = t26.R_NO and t26.DATE ='2016-01-26' left join t_listing_price as t27 on t26.R_NO = t27.R_NO and t27.DATE ='2016-01-27' left join t_listing_price as t28 on t27.R_NO = t28.R_NO and t28.DATE ='2016-01-28' left join t_listing_price as t29 on t28.R_NO = t29.R_NO and t29.DATE ='2016-01-29' left join t_listing_price as t30 on t29.R_NO = t30.R_NO and t30.DATE ='2016-01-30' left join t_listing_price as t31 on t30.R_NO = t31.R_NO and t31.DATE ='2016-01-31' where t1.DATE = '2016-01-01' AND t1.R_NO = '9021'
查询 RUN 后,结果显示:
| JAN 1 | JAN 2 | JAN 3 | JAN 4 | JAN 5 |
-----------------------------------------
| 300 | 300 | 500 | 0 | 600 |
现在我唯一的问题是我怎样才能在所有的日子里包括所有月份 (非常傻啊......:D)如果你不明白,我可以使用“代码示例”再次显示它的外观。 (相信我,我擅长这个:D)
| JAN 1 | JAN 2 | ... | JAN 31 | FEB 1 | ... | FEB 31 | (skip these months coz i hate 'em) | DEC 31 |
-----------------------------------------------------------------------------------------------------
| 300 | 300 | ... | 800 | 600 | ... | 300 | ... | 200 |
我需要一个改进的查询来在我的本地数据库上运行它并将其导出为 CSV 文件。 (将其视为报告文件)
就是这样。我希望你能帮助编辑/改进上面的PHP代码或查询,是的。