计算mysql联合类型查询

时间:2011-12-20 18:16:52

标签: php mysql

我有一个联合类型查询。如何计算它查询的行数?

mysql_query(
    "  (SELECT 1 as sort_col,performerid,pic0 
          FROM $table 
          WHERE performerid IS NOT NULL $performeridSql) 
    UNION 
       (SELECT 2 as sort_col,performerid,pic0 
          FROM $table 
          WHERE performerid IS NOT NULL 
              $categorySql $buildSql 
              $breastsize $haircolor $age $ethnicity 
              $willingnessSql) 
          ORDER BY sort_col");

2 个答案:

答案 0 :(得分:0)

只需计算子查询结果......

SELECT COUNT(t.*) FROM
(   (SELECT 1 AS sort_col,performerid,pic0 
       FROM $table 
       WHERE performerid IS NOT NULL 
         $performeridSql) 
  UNION 
    (SELECT 2 AS sort_col,performerid,pic0 
       FROM $table 
       WHERE performerid IS NOT NULL 
         $categorySql $buildSql 
         $breastsize $haircolor $age $ethnicity 
         $willingnessSql)
  ORDER BY sort_col)
) AS t

答案 1 :(得分:0)

如果除了计数之外还需要结果,mysqli_result::num_rowsPDOStatement::rowCount之类的内容将返回您想要的结果。否则,将查询作为子查询运行,然后将COUNT聚合函数应用于结果。