简体   繁体   English

MySQL在计算列的别名上使用内部联接

[英]MySQL using Inner joins on an alias of a calculated column

I have a query like so: 我有这样的查询:

SELECT User.id, 10*10 as distance
FROM USERS
INNER JOIN
(
    SELECT Location.user_id,
    min(10 * 10) as mindistance
    FROM Location
    GROUP BY Location.user_id
 ) L ON Users.id = Location.user_id AND distance = L.mindistance

If I leave it as is, I keep getting: 如果我保持原样,我会继续:

Unknown column 'distance' in 'on clause'

But if I put User.distance instead of just distance, I get: 但如果我把User.distance而不仅仅是距离,我得到:

MySQL syntax error near....

Can I not use alias' this way on a calculated field? 我不能在计算字段上以这种方式使用别名吗? The 10 * 10 is just a simple placeholder as the calculation is much more complex. 10 * 10只是一个简单的占位符,因为计算要复杂得多。

You are trying to use a column alias in the ON clause. 您正尝试在ON子句中使用列别名。 The MySQL documentation has this to say about this situation: MySQL文档有关于这种情况的说法:

The conditional_expr used with ON is any conditional expression of the form that can be used in a WHERE clause. ON使用的conditional_expr是可以在WHERE子句中使用的表单的任何条件表达式。

And also : 而且

Standard SQL disallows references to column aliases in a WHERE clause. 标准SQL不允许在WHERE子句中引用列别名。 This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined. 强制执行此限制是因为在评估WHERE子句时,可能尚未确定列值。 For example, the following query is illegal: 例如,以下查询是非法的:

That said, you can use variables to calculate the expression in the ON clause and then use the value in the column list, like this: 也就是说,您可以使用变量来计算ON子句中的表达式,然后使用列列表中的值,如下所示:

SELECT User.id, @dist as distance
FROM USERS
INNER JOIN
(
    SELECT Location.user_id,
    min(10 * 10) as mindistance
    FROM Location
    GROUP BY Location.user_id
 ) L ON Users.id = Location.user_id AND (@dist := 10*10) = L.mindistance

To avoid having to make the calculation three times in the query, you can wrap the outer calculation in a FROM subselect, which will give you access to the aliased field name (where it wasn't accessible in your original query): 为了避免在查询中进行三次计算,可以将外部计算包装在FROM选择中,这样您就可以访问别名字段名称(在原始查询中无法访问它):

SELECT a.*
FROM
(
    SELECT id, 10*10 AS distance
    FROM USERS
) a
INNER JOIN
(
    SELECT user_id,
    MIN(10 * 10) AS mindistance
    FROM Location
    GROUP BY user_id
) L ON a.id = L.user_id AND a.distance = L.mindistance

Here, the calculation is only done two times instead of three. 这里,计算只进行了两次而不是三次。

SELECT User.id, 10*10 as distance 
FROM USERS 
INNER JOIN 
( 
    SELECT Location.user_id, 
    min(10 * 10) as mindistance 
    FROM Location 
    GROUP BY Location.user_id 
 ) L ON User.id = Location.user_id AND L.mindistance =10*10

You can't used derived values in a query where clause - where is used to restrict records and which indexes to use - derived values can't be used by the optimizer so you need to filter the final results. 您不能在查询中使用派生值where子句 - 其中用于限制记录以及要使用的索引 - 优化程序无法使用派生值,因此您需要筛选最终结果。

not quite sure what you're doing but try something like: 不太清楚你在做什么,但尝试类似的东西:

SELECT User.id, 10*10 as distance 
FROM USERS 
INNER JOIN 
( 
    SELECT Location.user_id, 
    min(10 * 10) as mindistance 
    FROM Location 
    GROUP BY Location.user_id 
) L ON User.id = Location.user
HAVING USERS.distance = L.mindistance

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

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