目前我正在运行这样的 MySQL 查询
$query = "
SELECT
t1.id AS 'post_id',
t1.post_title AS 'post_title',
t1.post_status AS 'post_status',
COUNT( t1.id ) AS 'row_count', //this one is not working
COUNT( t2.tag_id ) AS 'link_count',
GROUP_CONCAT( t2.tag_id) AS 'link_id',
GROUP_CONCAT( t2.url) AS 'url',
GROUP_CONCAT( t2.title) AS 'title',
GROUP_CONCAT( t2.active) AS 'active',
FROM $posts_table AS t1
JOIN $tags_table AS t2 ON ( t1.id = t2.post_id )
GROUP BY t1.id
ORDER BY t1.id DESC
LIMIT $start_from, $row_to_show;
";
这个查询的每个部分都执行得很好,除了COUNT( t1.id ) AS 'row_count',
行。
我想计算在t1 表中找到的总行数,但是当我转储数组时
$result = $wpdb->get_results($query);
var_dump($result->row_count);
它给了我NULL
。
如果我在循环中使用它
foreach ( $result as $result ){
echo $result->row_count;
}
link_count
然后该值与下一行中声明的值相同,COUNT( t2.link_id ) AS 'link_count',
给出了我想要的值。
请给我一些关于如何在此查询上使用分页(如 30 个结果中的 10 个)的想法。