我有这样的SQL查询基本上我从一个SQL专家那里得到它:
SELECT
if(pd1.recycle_bin >1, pd1.project_id, t.project_id) AS project_id,
if(pd1.recycle_bin >1, pd1.project_name, t.project_name) AS project_name,
if(pd1.recycle_bin >1, pd1.parent_id, t.parent_id) AS parent_id,
if(pd1.recycle_bin >1, pd1.recycle_bin, t.recycle_bin) AS recycle_bin,
if(pd1.recycle_bin >1, pd1.creater_id, t.creater_id) AS creater_id
FROM projectdetails AS pd1
JOIN(
SELECT * FROM projectdetails AS pd
WHERE pd.parent_id > 0
AND pd.recycle_bin > 1
) AS t ON t.parent_id = pd1.project_id
GROUP BY project_id;
什么是等效的codeigniter sql查询?
答案 0 :(得分:0)
使用有效记录,您可以这样做
$this->db
->select('*')
->from('projectdetails pd')
->where('pd.parent_id > 0 AND pd.recycle_bin > 1',null,FALSE);
$joinquery = $this->db->_compile_select();
$this->db->_reset_select();
$query=$this->db
->select(' if(pd1.recycle_bin >1, pd1.project_id, t.project_id) AS project_id,
if(pd1.recycle_bin >1, pd1.project_name, t.project_name) AS project_name,
if(pd1.recycle_bin >1, pd1.parent_id, t.parent_id) AS parent_id,
if(pd1.recycle_bin >1, pd1.recycle_bin, t.recycle_bin) AS recycle_bin,
if(pd1.recycle_bin >1, pd1.creater_id, t.creater_id) AS creater_id' ,FALSE)
->from('projectdetails pd1')
->join('($joinquery) t','t.parent_id = pd1.project_id')
->group_by('project_id')
->having('creater_id= 7923')
->get();
或者只使用query()
功能
$query =$this->db->query('your full query');
根据评论进行修改
$this->db->query('SELECT
if(pd1.recycle_bin >1, pd1.project_id, t.project_id) AS project_id,
if(pd1.recycle_bin >1, pd1.project_name, t.project_name) AS project_name,
if(pd1.recycle_bin >1, pd1.parent_id, t.parent_id) AS parent_id,
if(pd1.recycle_bin >1, pd1.recycle_bin, t.recycle_bin) AS recycle_bin,
if(pd1.recycle_bin >1, pd1.creater_id, t.creater_id) AS creater_id
FROM projectdetails AS pd1
JOIN(
SELECT * FROM projectdetails AS pd
WHERE pd.parent_id > 0
AND pd.recycle_bin > 1
) AS t ON t.parent_id = pd1.project_id
GROUP BY project_id
HAVING creater_id= 7923
');