如何计算此sql语句中的行?

时间:2016-03-19 18:51:50

标签: sql zend-framework count

你能否回答以下问题来帮助我? 如何计算此sql语句中的行?

SELECT `u`.*, 
   ( 6371 * Acos(Cos(Radians(51.6992)) * Cos(Radians(localization_lat)) * 
                               Cos(Radians(localization_lng) - Radians( 
                                   5.3042)) + 
                 Sin 
                   ( 
                          Radians(51.6992)) * Sin(Radians(localization_lat)) 
            ) ) AS 
   `distance` 
FROM   `ads` AS `u` 
WHERE  ( localization_zip_code LIKE '%5200%' ) 
       AND ( date_end > '2016-03-19 19:34:43' 
             AND date_start < '2016-03-19 19:34:43' ) 
       AND ( is_show = 1 ) 
       AND ( is_accept_admin = 1 ) 
       AND ( is_in_category_page = 1 ) 
HAVING ( `distance` < '70' ) 
ORDER  BY `distance` ASC 

3 个答案:

答案 0 :(得分:0)

select count(*) from tableselect count(column name) from table方法返回记录数 尝试

SELECT Count(*), 
   `u`.*, 
   ( 6371 * Acos(Cos(Radians(51.6992)) * Cos(Radians(localization_lat)) * 
                               Cos(Radians(localization_lng) - Radians( 
                                   5.3042)) + 
                 Sin 
                   ( 
                          Radians(51.6992)) * Sin(Radians(localization_lat)) 
            ) ) AS 
   `distance` 
FROM   `ads` AS `u` 
WHERE  ( localization_zip_code LIKE '%5200%' ) 
       AND ( date_end > '2016-03-19 19:34:43' 
             AND date_start < '2016-03-19 19:34:43' ) 
       AND ( is_show = 1 ) 
       AND ( is_accept_admin = 1 ) 
       AND ( is_in_category_page = 1 ) 
HAVING ( `distance` < '70' ) 
ORDER  BY `distance` ASC 

答案 1 :(得分:0)

是的我知道,但是当我把count()放在声明中时,我得到0结果而没有计数()我在这种情况下得到两个...... 我可以在一个sql语句中使用count()和HAVING吗?

答案 2 :(得分:0)

您应该可以将其作为子查询SELECT COUNT(*) FROM (...) AS mysubquery来执行。所以可能是这样的:

SELECT COUNT(*) FROM (
  SELECT `u`.*,
     ( 6371 * Acos(Cos(Radians(51.6992)) * Cos(Radians(localization_lat)) *
                                 Cos(Radians(localization_lng) - Radians(
                                     5.3042)) +
                   Sin
                     (
                            Radians(51.6992)) * Sin(Radians(localization_lat))
              ) ) AS
     `distance`
  FROM   `ads` AS `u`
  WHERE  ( localization_zip_code LIKE '%5200%' )
         AND ( date_end > '2016-03-19 19:34:43'
               AND date_start < '2016-03-19 19:34:43' )
         AND ( is_show = 1 )
         AND ( is_accept_admin = 1 )
         AND ( is_in_category_page = 1 )
  HAVING ( `distance` < '70' )
  ORDER  BY `distance` ASC
  ) AS mysubquery