【问题标题】:How to add condition on virtual column in mysql?如何在mysql中的虚拟列上添加条件?
【发布时间】:2020-11-22 03:52:25
【问题描述】:

我想为 tbl_restaurant_featured_history.id 列添加条件,但我无法在 where 子句中添加该条件,因为它显示错误提示 Unknown column 'featured' 在“where 子句”中,如果我在有子句中添加条件 featured is not null 它返回 0 行

下面是添加条件前的查询

SELECT 
  DISTINCT(tbl_restaurant.id) as restaurant_id,  
  tbl_restaurant.name,  
  tbl_restaurant_featured_history.id as featured, 
  tbl_restaurant.min_order_amount,  
  tbl_restaurant.latitude as latitude,  
  tbl_restaurant.logo, 
  tbl_favourite_restaurant.id as is_fav, 
  tbl_restaurant.address as address,  
  IF(tbl_restaurant_timing.start_time <= '19:56:26' && tbl_restaurant.service = 'Available' && tbl_restaurant_timing.end_time >= '19:56:26', 'Open', 'Closed') AS availblity,
  tbl_restaurant.longitude as longitude,  
  (
    SELECT ROUND(AVG(tbl_rate_review.rate)) 
    FROM tbl_rate_review 
    where tbl_rate_review.restaurant_id = tbl_restaurant.id 
    GROUP BY restaurant_id
  ) as avgrating, 
  (
    SELECT ROUND(AVG(tbl_rate_review.rate), 2) 
    FROM tbl_rate_review 
    where tbl_rate_review.restaurant_id = tbl_restaurant.id 
    GROUP BY restaurant_id
  ) as rating,  
  111.045 * DEGREES(ACOS(COS(RADIANS(23.0266941)) * COS(RADIANS(latitude)) * COS(RADIANS(longitude) - RADIANS(72.6008731)) + SIN(RADIANS(23.0266941)) * SIN(RADIANS(latitude)))) AS distance_in_km 
FROM tbl_restaurant 
LEFT JOIN tbl_restaurant_featured_history ON tbl_restaurant_featured_history.restaurant_id = tbl_restaurant.id 
LEFT JOIN tbl_restaurant_menu ON tbl_restaurant_menu.restaurant_id = tbl_restaurant.id AND tbl_restaurant_menu.status='Active' 
LEFT JOIN tbl_favourite_restaurant ON tbl_favourite_restaurant.restaurant_id=tbl_restaurant.id AND tbl_favourite_restaurant.user_id=19 
LEFT JOIN tbl_restaurant_timing ON tbl_restaurant_timing.restaurant_id = tbl_restaurant.id AND tbl_restaurant_timing.day = 'Saturday' 
WHERE tbl_restaurant.status = 'Active'  
HAVING distance_in_km <= 10  
ORDER BY availblity DESC, distance_in_km ASC LIMIT 10, 10

以及这个查询的输出

【问题讨论】:

  • 请努力正确格式化您的查询。格式化栏中有一个按钮可以将文本格式化为代码。
  • 非常感谢@GMB!现在看起来格式化了吗?

标签: mysql select where-clause having-clause


【解决方案1】:

tbl_restaurant_featured_historyLEFT 连接到表tbl_restaurant,这就是为什么结果中出现空值的原因,因为某些行与您设置的ON 子句的条件不匹配。
如果要添加条件:

tbl_restaurant_featured_history.id is not null

这意味着您只需要匹配的行,并且从您的示例数据中我看到只有 1 个匹配的行。
在这种情况下,您只需将连接更改为 INNER 连接:

.................................
FROM tbl_restaurant 
INNER JOIN tbl_restaurant_featured_history ON tbl_restaurant_featured_history.restaurant_id = tbl_restaurant.id
.................................

【讨论】:

    【解决方案2】:

    查询的格式很差,因此很难理解。

    我可以在select 子句中看到这一点:

    tbl_restaurant_featured_history.id as featured
    

    查询的where 子句不能引用select 子句中定义的别名。如果要对此进行过滤,则需要使用列名 (tbl_restaurant_featured_history.id) 而不是别名 (featured):

    where tbl_restaurant_featured_history.id is not null
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-18
      相关资源
      最近更新 更多