在mysql查询中使用LIMIT和UNION时获取总行数

时间:2014-07-15 09:00:09

标签: php mysql

Query:

SELECT
    tbl_a.id as aid,
    tbl_a.name as aname,
    null location,
    .....
    .....
    from tbl_a
    .........//left join to fetch some other data
    .........//where condition
UNION ALL
    0 aid,
    null aname,
    tbl_b.location as location,
    .....
    .....
    from tbl_b
    .........//left join to fetch some other data
    .........//where condition
limit '.$recordperpage.' OFFSET '.$offset.'

此处,$recordperpage$offset是动态的。

现在我想要取得所有不。没有限制的行。

  

EXA:
  限制行数:20
  没有限制的行数:50
  我想要取消。没有限制的行(装置50)。

那么如何实现呢? 提前谢谢。

2 个答案:

答案 0 :(得分:0)

也许SQL_CALC_FOUND_ROWS适合您? 看看这里。

How to count all records but only retrieve (LIMIT) a specific number for display?

答案 1 :(得分:0)

此查询可以解决问题:

SELECT count(*) as count
FROM
(SELECT
    tbl_a.id as aid,
    tbl_a.name as aname,
    null location,
    .....
    .....
    from tbl_a
    .........//left join to fetch some other data
    .........//where condition
UNION ALL
    0 aid,
    null aname,
    tbl_b.location as location,
    .....
    .....
    from tbl_b
    .........//left join to fetch some other data
    .........//where condition
) union_table
相关问题