目前我正在一個MySQL查詢像這樣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;
}
內,則值是相同在非常下一行COUNT(t2.link_id) AS 'link_count',
宣佈link_count
給我想要的值。
請給我一些關於如何在此查詢中使用分頁的想法(如30個結果中的10個)。
檢查是否包含全局$ wpdb; – swapnesh
是的,沒有'$ wpdb'的問題,查詢工作正常,除了我上面提到的那一行。我必須做錯了什麼,不能找出那是什麼.. :( – Anuj
t1.id AS'post_id',現在你又指t1.id讓它post_id並讓我知道:) – swapnesh