需要帮助来构建一个codeigniter数据库查询

时间:2015-09-27 16:29:59

标签: php mysql codeigniter

我想使用codeigniter query_builder函数。没有想法,请帮助。

$this->db->query("SELECT city as popularcity FROM(SELECT city,count(city) as
countcity FROM ads LIMIT 0, 6) as t ORDER BY MAX(countcity)");

2 个答案:

答案 0 :(得分:0)

$this->db->query("SELECT city as popularcity, countcity 
FROM 
(SELECT city, count(city) as countcity FROM ads GROUP BY city) as t
ORDER BY countcity DESC LIMIT 0, 6");

答案 1 :(得分:0)

尝试以下代码,

$this->db->select("city as popularcity, countcity"); 
$this->db->from("(SELECT city, count(city) as countcity FROM ads GROUP BY city) as t");
$this->db->order_by("countcity DESC");
$this->db->limit(0,6);