PHP mysql选择带CASE条件的查询

时间:2015-12-29 11:33:00

标签: php mysql

我是餐厅,餐厅时间表。我需要证明餐厅是开放的还是关闭的。

这是我的查询

$cur_nowtime = strtotime(date("H:i"));
$curDayOct   = strtolower(date("D"));

SELECT rest.restaurant_id, rest.restaurant_name, 
       restime.".$curDayOct."_status AS res_curr_ocday_status, 
       IF( ".$cur_nowtime." BETWEEN ".$curDayOct."_ot AND ".$curDayOct."_ct, 'Open','Closed') AS res_oct_status, 
       CASE WHEN res_curr_ocday_status = 'Open' THEN 'Open'
       WHEN res_oct_status = 'Open' THEN 'Open'
       ELSE 'Closed' END AS final_res_status
FROM restaurantAS rest 
LEFT JOIN restaurant_time AS restime ON rest.restaurant_id = restime.res_id

我需要在" final_res_status"的领域获得餐厅的openclose身份。使用2个alies name" res_curr_ocday_status"和" res_oct_status"

我需要将输出显示如下。

restaurant_id   restaurant_name res_curr_ocday_status   res_oct_status  final_res_status
1   aaa     Open    Closed  Closed
2   bbbb    Open    Open    Open
3   cccc    Closed  Closed  Closed
4   dddd    Closed  Open    Closed

请查看我的查询..

SELECT temp.*, (CASE WHEN temp.res_curr_ocday_status = 'Open' AND temp.res_curr_octime_status = 'Open' THEN 'Open'
ELSE 'Closed' END) AS res_oct_status FROM 
(SELECT rest.restaurant_id, rest.restaurant_name,
   restime.".$curDayOct."_status AS res_curr_ocday_status, 
   IF( ".$cur_nowtime." BETWEEN ".$curDayOct."_ot AND ".$curDayOct."_ct, 'Open','Closed') AS res_curr_octime_status,
   cty.cityname, zip.zipcode,
   ROUND( ( ( SUM(rvw.rating) / (COUNT(rvw.rating_id)*5) )*100 ) ,1) AS avg_rating
   FROM restaurant AS rest 
   LEFT JOIN restaurant_time AS restime ON rest.restaurant_id = restime.res_id 
   WHERE rest.restaurant_status = '1' GROUP BY rest.restaurant_id ORDER BY FIELD(res_oct_status, 'Open') DESC , FIELD(rest.restaurant_zip, '2600') DESC, RAND() LIMIT 0,10 ) AS temp

...我正在获取"未知列' res_oct_status'在'订单条款'"

2 个答案:

答案 0 :(得分:0)

试试这个(第一个是我修改的查询,第二个是我之前的回答):

SELECT temp.*, (CASE WHEN temp.res_curr_ocday_status = 'Open' AND temp.res_curr_octime_status = 'Open' THEN 'Open'
ELSE 'Closed' END) AS res_oct_status FROM 
(SELECT rest.restaurant_id, rest.restaurant_name,
   restime.".$curDayOct."_status AS res_curr_ocday_status, 
   IF( ".$cur_nowtime." BETWEEN ".$curDayOct."_ot AND ".$curDayOct."_ct, 'Open','Closed') AS res_curr_octime_status,
   cty.cityname, zip.zipcode,
   ROUND( ( ( SUM(rvw.rating) / (COUNT(rvw.rating_id)*5) )*100 ) ,1) AS avg_rating
   FROM restaurant AS rest 
   LEFT JOIN restaurant_time AS restime ON rest.restaurant_id = restime.res_id 
   WHERE rest.restaurant_status = '1' GROUP BY rest.restaurant_id  ) AS temp ORDER BY FIELD(temp.res_oct_status, 'Open') DESC , FIELD(temp.restaurant_zip, '2600') DESC, RAND() LIMIT 0,10
    SELECT temp.*, (CASE WHEN temp.res_curr_ocday_status = 'Open' THEN 'Open'
           WHEN temp.res_oct_status = 'Open' THEN 'Open'
           ELSE 'Closed' END) AS final_res_status FROM 
    (SELECT rest.restaurant_id, rest.restaurant_name, 
    restime.".$curDayOct."_status AS res_curr_ocday_status, 
    IF( ".$cur_nowtime." BETWEEN ".$curDayOct."_ot AND ".$curDayOct."_ct, 'Open','Closed') AS res_oct_status, 
    FROM restaurantAS rest 
    LEFT JOIN restaurant_time AS restime ON rest.restaurant_id = restime.res_id
LEFT JOIN city AS cty ON rest.restaurant_city = cty.city_id AND rest.restaurant_status = '1') as temp ORDER BY FIELD(final_res_status , 'Open') DESC

答案 1 :(得分:-1)

使用以下sql查询:

SELECT temp.*, (CASE WHEN temp.res_curr_ocday_status = 'Open' AND temp.res_oct_status = 'Open' THEN 'Open'
       ELSE 'Closed' END) AS final_res_status FROM 
(SELECT rest.restaurant_id, rest.restaurant_name, 
restime.".$curDayOct."_status AS res_curr_ocday_status, 
IF( ".$cur_nowtime." BETWEEN ".$curDayOct."_ot AND ".$curDayOct."_ct, 'Open','Closed') AS res_oct_status, 
FROM restaurantAS rest 
LEFT JOIN restaurant_time AS restime ON rest.restaurant_id = restime.res_id) as temp