php代码使用if esle匹配列值

时间:2015-05-16 14:16:31

标签: php mysql sql

我有两张桌子......

holidaymaster

  id   holidaydate  cityid
   1    18-04-2015    9

收据表

 id     coupondate     cityname   customer
 1       15-04-2015       9         A
 2       16-04-2015       9         B
 3       17-04-2015       9         C
 4       19-04-2015       9         D

我需要通过加入这个来从两个表中获取数据......

并且需要在holidaymaster中检查holidaydate是否为空白...然后在此日期回显null,否则回显1。

预期输出

customer     coupondate    Type 
 A           15-04-2015    1 
 B           16-04-2015    1
 C           17-04-2015    1
 NULL        NULL          NULL
 D           19-04-2015    1

在abov ex - 2015年4月18日可以在holidaymaster中使用,所以我需要在这个显示屏上显示null。

plz帮助我如何加入这两个表并匹配行......

如果无法加入,那么如何使用if else匹配两个不同的查询..

下面是我的代码..

我尝试使用2 diff查询..

$data = $database->getRows("SELECT RE.* FROM receipt_entry RE LEFT JOIN city_master CM ON RE.city_name = CM.id WHERE CM.cityname = :cityname
        AND str_to_date(RE.coupondate,'%d-%m-%Y') BETWEEN :fromdate AND :todate ORDER BY STR_TO_DATE(RE.coupondate,'%d-%m-%Y') ASC",
        array(':fromdate'=>$formdate_sql,':todate'=>$todate_sql,':cityname'=>$cityname));

        $holiday = $database->getRows("SELECT HM.holidaydate from holidaymaster HM LEFT JOIN city_master CM ON HM.cityid = CM.id WHERE CM.cityname = :cityname
        AND str_to_date(HM.holidaydate,'%d-%m-%Y') BETWEEN :fromdate AND :todate",array(':fromdate'=>$formdate_sql,':todate'=>$todate_sql,':cityname'=>$cityname)); 



<?php  if (is_array($data)) { foreach($data as $row)    {   ?>

<?php foreach($holiday as $row1)    {   ?>

<?php if($row['coupondate'] == $row1['holidaydate']) {  ?>
        <td>-</td>
        <?php }  else { ?>
        <td>1</td>
        <?php } ?>      

        <?php } } ?>

1 个答案:

答案 0 :(得分:1)

SELECT
    customer, coupondate, IF(holidaydate is null, null, 1) AS type
FROM holidaymaster h
JOIN receiptentry r ON h.cityid = r.cityname
;