简体   繁体   English

如何在 mysql 的虚拟列上添加条件?

[英]How to add condition on virtual column in mysql?

I want to add a condition for the tbl_restaurant_featured_history.id column but I can't add that condition in where clause because It shows an error saying Unknown column 'featured' in 'where clause' and If I add a condition featured is not null in having clause It is returning 0 rows .我想为tbl_restaurant_featured_history.id列添加一个条件,但我无法在 where 子句中添加该条件,因为它显示了一个错误,提示“where 子句”中的Unknown column 'featured'并且如果我添加一个条件featured is not null in有子句它返回0 行

Below is the query before adding the condition以下是添加条件之前的查询

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

And the output of this query而这个查询的 output

在此处输入图像描述

The query is poorly formated and hence rather hard to follow.该查询格式不正确,因此很难理解。

I can see this in the select clause:我可以在select子句中看到这一点:

tbl_restaurant_featured_history.id as featured

The where clause of a query just can't refer to an alias defined in the select clause.查询的where子句不能引用select子句中定义的别名。 If you want to filter on this, then you need to use the column name ( tbl_restaurant_featured_history.id ) rather than the alias ( featured ):如果要对此进行过滤,则需要使用列名 ( tbl_restaurant_featured_history.id ) 而不是别名 ( featured ):

where tbl_restaurant_featured_history.id is not null

The table tbl_restaurant_featured_history is LEFT joined to the table tbl_restaurant and this is why you get nulls in the results because some rows do not match the conditions of the ON clause that you have set.tbl_restaurant_featured_history LEFT连接到表tbl_restaurant ,这就是为什么您在结果中得到空值的原因,因为某些行与您设置的ON子句的条件不匹配。
If you want to add the condition:如果要添加条件:

tbl_restaurant_featured_history.id is not null

this means that you want only the matching rows and from your sample data I see that there is only 1 matching row.这意味着您只需要匹配的行,并且从您的示例数据中我看到只有 1 个匹配的行。
In this case all you have to do is change the join to an INNER join:在这种情况下,您只需将连接更改为INNER连接:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM