如何以Codeigniter風格編寫以下查詢。以codeigniter風格編寫聯合查詢
SELECT COUNT(`id`) AS reccount
FROM
(SELECT `id` FROM table1
WHERE tid= '101' AND `status` = 1
UNION ALL
SELECT `id` FROM table2
WHERE tid= '101' AND `status` = 1
UNION ALL
SELECT `id` FROM table3
WHERE tid= '101' AND `status` = 1) t
我用下面的方法來執行它。
這是唯一正確的方法還是您有任何改進建議?
$q = $this->db->query(SELECT COUNT(`id`) AS reccount
FROM
(SELECT `id` FROM table1
WHERE tid= '101' AND `status` = 1
UNION ALL
SELECT `id` FROM table2
WHERE tid= '101' AND `status` = 1
UNION ALL
SELECT `id` FROM table3
WHERE tid= '101' AND `status` = 1) t ");
可能重複http://stackoverflow.com/questions/2040655/ union-query-with-codeigniters-active-record-pattern) – 2014-09-30 19:19:44