dousu8767 2015-06-10 12:59
浏览 42
已采纳

MySQL与AS结合HAVING的困难

I have an AS-related MySQL question, specifically when combined with HAVING:

This does work (mind the "(250) AS", second line):

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;

This does NOT work:

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;

The strange thing is that it doesn't give me any errors but that it simply returns 4 of the very same rows/records, instead of 4 different ones.

Pretty clueless here..

Above is the simplified version of my real problem.. Where I have (200) in my example above, I'm actually trying to measure the distance value between longitude and latitude values:

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;

Now this works as long as I don't use HAVING distance < 20 ORDER BY distance .. But as soon as I use these, I get the 4 very same results, instead of the expected 4 different results.

But, in my simplified example, I'm having the exact same issue.

[SOLUTION] This is what did the trick and provided the functionallity what I need - as proposed by @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;

Thanks!!!

  • 写回答

1条回答 默认 最新

  • dongwupu5991 2015-06-10 13:24
    关注
    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;
    

    Note that squaresize is hard coded and useless in your top chunk that works as u say. The 250 is known by the front-end

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题