duanheye7423 2014-04-30 14:07
浏览 70
已采纳

使用UNION进行三次查询

I'm trying to merge three separate queries into one using the UNION function of MYSQL.

I want to output one table of data that contains the booking_count, reserve_count and cancel_count. Some rows will have values for all three, some will only have values for one.

I wonder if anyone could point out where I'm going wrong?

The errors I am getting are:

Notice: Undefined index: cancel_count Notice: Undefined index: reserve_count

I assume this is a problem with the MYSQL query as the PHP looks fine

$query = "SELECT id, class_date, class_id, COUNT(*) AS booked_count   
          FROM bookings
          WHERE booking_status='#BOOKED#'
          UNION ALL

          SELECT id, class_date, class_id, COUNT(*) AS reserve_count  
          FROM bookings
          WHERE booking_status='#RESERVE#'
          UNION ALL

      SELECT id, class_date, class_id, COUNT(*) AS cancel_count  
          FROM bookings
          WHERE booking_status='#CANCELLED#'
          GROUP BY class_date, class_id
          ORDER BY class_date ASC, class_id ASC" ;

$result = mysqli_query($sql,$query);

while($row = mysqli_fetch_assoc($result)) {
   $union[$row['id']] = array('class_date' => $row['class_date'], 'class_id' => $row['class_id'], 'booked_count' => $row['booked_count'], 'reserve_count' => $row['reserve_count'], 'cancel_count' => $row['cancel_count']);
}

echo '<table><tr><th>id</th><th>class_id</th><th>class_date</th><th>booking_number</th><th>reserve_number</th><th>cancelled_number</th></tr>';

foreach($union as $union) {
   echo '<tr>';
   echo '<td>&nbsp;</td>'; // id
   echo '<td>'.$union['class_id'].'</td>';
   echo '<td>'.$union['class_date'].'</td>';
   echo '<td>'.$union['booked_count'].'</td>';
   echo '<td>'.$union['reserve_count'].'</td>';
   echo '<td>'.$union['cancel_count'].'</td>';
   echo '</tr>';
}

echo '</table>';
  • 写回答

2条回答 默认 最新

  • dtx3006 2014-04-30 17:02
    关注

    I think something like this would work:

    SELECT id, class_date, class_id,
      SUM(booked_count) AS booked_count,
      SUM(reserve_count) AS reserve_count,
      SUM(cancel_count) AS cancel_count
    FROM
    (SELECT id, class_date, class_id,
      1 AS booked_count,
      0 AS reserve_count,
      0 AS cancel_count 
    FROM bookings
    WHERE booking_status = '#BOOKED#'
    UNION ALL
    SELECT id, class_date, class_id,
      0 AS booked_count,
      1 AS reserve_count,
      0 AS cancel_count 
    FROM bookings
    WHERE booking_status = '#RESERVE#'
    UNION ALL
    SELECT id, class_date, class_id,
      0 AS booked_count,
      0 AS reserve_count,
      1 AS cancel_count  
    FROM bookings
    WHERE booking_status = '#CANCELLED#') AS t
    GROUP BY class_date, class_id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度