【问题标题】:mysql query question about having and countmysql关于拥有和计数的查询问题
【发布时间】:2021-12-28 21:14:06
【问题描述】:

在这个查询*中,我按纬度、经度、各种标准……从近到远或得分及其工作/列出数据列出并排序“mytableabc”中的行,我认为它足够好/足够快!?

select 
round((6371 * acos(cos(radians('40.123456')) * cos(radians(latitude)) * cos(radians(longtitude) - radians('29.123456')) + sin(radians(latitude)) * sin(radians('40.123456')))), (2)) as distance, 
    (match(colone,coltwo) against('searchkeywordabc' in boolean mode)) as score, 
id,colone,coltwo,latitude,longtitude,colthree 
        from mytableabc 
        where (colone='sampleforsomething') 
            and (match(colone,coltwo) against('searchkeywordabc' in boolean mode))
                having distance <= 5 order by distance asc limit 0,50

--问题

在不同的行和文件中,我只需要具有相同条件的返回元素的总数 count(id)... 我尝试添加计数(id),但它没有给出正确的计数总数和列表行,我只想要总数(就像通常计数查询一样)而不是数据行

select 
    count(id), 
round((6371 * acos(cos(radians('40.123456')) * cos(radians(latitude)) * cos(radians(longtitude) - radians('29.123456')) + sin(radians(latitude)) * sin(radians('40.123456')))), (2)) as distance, 
        (match(colone,coltwo) against('searchkeywordabc' in boolean mode)) as score, 
    id,colone,coltwo,latitude,longtitude,colthree 
            from mytableabc 
            where (colone='sampleforsomething') 
                and (match(colone,coltwo) against('searchkeywordabc' in boolean mode))
                    having distance <= 5 order by distance asc limit 0,50

--这个没有距离/纬度/经度标准的查询也很好用,只返回总数......但我需要在这个查询中添加距离查询

select count(id) from mytableabc where (colone='sampleforsomething') and (match(colone,coltwo) against('searchkeywordabc' in boolean mode))

我不是 sql 专家,是的,正如您所见,我需要一个。 谢谢。

【问题讨论】:

    标签: php mysql geolocation mariadb


    【解决方案1】:

    A 计划:

    distance 检查移至WHERE 子句。这消除了对HAVING 的需要,同时过滤更多,因此COUNT 不会被夸大。

    select  count(*)
        from  mytableabc
        where  (colone='sampleforsomething')
          and  (match(colone,coltwo) against('searchkeywordabc' in boolean mode))
          AND  (6371 * acos(cos(radians('40.123456')) * 
            cos(radians(latitude)) * cos(radians(longtitude) -
            radians('29.123456')) + sin(radians(latitude)) *
            sin(radians('40.123456')))) < 5;
    

    我希望首先使用 FULLTEXT 索引,然后检查其他两个测试(= 和距离)以检查 FT 搜索返回的所有行。

    B计划:

    继续使用HAVING,但有一个子查询。 (这可能更慢。)

    【讨论】:

    • 我分析/基准并计划一个选定的。感谢您的快速回复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    相关资源
    最近更新 更多