简体   繁体   English

我需要一张临时桌子吗?

[英]Do I need a temporary table?

I have the following query and I need to add "and distance < 10" to the where clause. 我有以下查询,我需要在where子句中添加“ and distance <10”。 Because 'distance' is computed variable it can't be used in where clause. 由于“距离”是计算变量,因此不能在where子句中使用。 I have tried HAVING instead of where but that breaks the replace part. 我尝试了HAVING而不是在哪里,但是这破坏了替换部分。

I think the answer is to use a temporary table for the distance computation but i can't figure out the syntax as everything I have tried doesn't work. 我认为答案是使用一个临时表进行距离计算,但是我无法弄清楚语法,因为我尝试过的所有方法都不起作用。

All help appreciated please. 感谢所有帮助。 Thanks. 谢谢。

select
    Contractor.contractorID,
    Contractor.firstName,
    Contractor.lastName,
    Contractor.emailAddress,
    Contractor.nationality,
    Contractor.dateOfBirth,
    Contractor.address1,
    Contractor.address2,
    Contractor.city,
    Contractor.county,
    Contractor.postcode,
    Contractor.country,
    Contractor.tel,
    Contractor.mob,
    postcodes.Grid_N Grid_N1,
    postcodes.Grid_E Grid_E1,
    (select Grid_N from postcodes where pCode='".$postcode."') Grid_N2,
    (select Grid_E from postcodes where pCode='".$postcode."') Grid_E2,
    ( (select sqrt(((Grid_N1-Grid_N2)*(Grid_N1-Grid_N2))+((Grid_E1-Grid_E2)*(Grid_E1-Grid_E2))) ))/1000*0.621371192 as distance
from 
    Contractor,
    postcodes 
where 
    postcodes.Pcode = replace(substring(Contractor.postcode,1,length(Contractor.postcode)-3),'','') 
order by 
    distance asc

In MySQL, you can do this with: 在MySQL中,您可以执行以下操作:

where . . .
having distance < 10
order by distance;

You don't need to put the other conditions in the having clause. 您无需将其他条件放在having子句中。 Also, your query could benefit from using ANSI standard join syntax (using the on clause, for instance). 同样,使用ANSI标准联接语法(例如,使用on子句)可以使查询受益。

SELECT list
     , of
     , columns
     , including
     , distance
FROM   (
        <your existing query>
        /* minus ORDER BY clause (needs to be on outermost query */
       ) As a_subquery
WHERE  distance < 10
ORDER
    BY some_stuff

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

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