MySQL与AS结合HAVING的困难

时间:2015-06-10 12:59:22

标签: php mysql sql

我有一个与AS相关的MySQL问题,特别是当与HAVING结合使用时:

确实工作(请注意"(250)AS",第二行):

SELECT A.uid, A.name, A.height, A.width,
(250) AS squaresize
FROM tx_fach_domain_model_professional A,
tx_fach_domain_model_category B,
tx_fach_professional_category_mm MM
WHERE MM.uid_local = A.uid and
MM.uid_foreign = 2
HAVING squaresize > 20
ORDER BY squaresize LIMIT 0 , 4;

NOT 工作:

SELECT A.uid, A.name, A.height, A.width,
(A.height * A.width) AS squaresize
FROM tx_fach_domain_model_professional A,
tx_fach_domain_model_category B,
tx_fach_professional_category_mm MM
WHERE MM.uid_local = A.uid and
MM.uid_foreign = 2
HAVING squaresize > 20
ORDER BY squaresize LIMIT 0 , 4;

奇怪的是,它并没有给我任何错误,但它只返回4个相同的行/记录,而不是4个不同的行。

这里很无能......

以上是我真实问题的简化版本。在上面的示例中我有(200),我实际上是在测量经度和纬度值之间的距离值:

SELECT A.uid, A.name, A.latitute, A.longitute,
( 6371 * acos ( cos ( radians(52.52000659999999) ) 
* cos( radians( A.latitute ) ) * cos( radians( A.longitute ) - 
radians(13.404953999999975) ) + sin ( radians(52.52000659999999) ) * sin( 
radians( A.latitute ) ) ) ) AS distance
FROM tx_fach_domain_model_professional A, 
tx_fach_domain_model_category B, 
tx_fach_professional_category_mm MM WHERE MM.uid_local = A.uid and 
MM.uid_foreign = 2 HAVING distance < 20 ORDER BY distance LIMIT 0 , 4;

现在,只要我不使用距离&lt; 20 ORDER BY distance ..但是一旦我使用这些,我得到4个非常相同的结果,而不是预期的4 不同的结果。

但是,在我的简化示例中,我遇到了完全相同的问题。

[解决方案] 这就是诀窍,并提供了我需要的功能 - 正如@AsConfused所提出的那样:

SELECT A.uid, A.name, A.latitute, A.longitute,
( 6371 * acos ( cos ( radians(52.52000659999999) )
* cos( radians( A.latitute ) ) * cos( radians( A.longitute ) 
radians(13.404953999999975) ) + sin ( radians(52.52000659999999) ) *
sin(radians( A.latitute ) ) ) ) AS distance
FROM tx_fach_domain_model_professional A JOIN
tx_fach_professional_category_mm MM
on MM.uid_local = A.uid and MM.uid_foreign = 2
HAVING distance < 20
ORDER BY distance LIMIT 0 , 4;

感谢!!!

1 个答案:

答案 0 :(得分:1)

SELECT A.uid, A.name, A.height, A.width
FROM tx_fach_domain_model_professional A
Join tx_fach_professional_category_mm MM
on MM.uid_local = A.uid and
MM.uid_foreign = 2
LIMIT 0 , 4;

请注意,squareize是硬编码的,并且在你的顶部块中没用,就像你说的那样。 250是前端知道的