所以我正在研究一个现有系统,我正在试图找出他们如何设置mysql。他们有像这样的查询设置
$this->db->select('nID, nAID');
$this->db->where('bValid', 1);
$this->db->where('nID', (int)$nID);
$this->db->from('event');
$this->db->group_by('nID, nAID');
$query = $this->db->get();
此查询效果很好,但我需要设置子查询。我已经在SQL中编写了查询并对其进行了测试,以确认它是否有效,但我无法弄清楚如何翻译它。基本上我需要的不是从'事件'表中抓取,而是需要从我之前做出的子选择中获取。我怎么想象它会是。
$this->db->select('nID, nAID');
$this->db->where('bValid', 1);
$this->db->where('nID', (int)$nID);
$this->db->from(
$this->db->select('something, somethingelse');
$this->db->where('something', 1);
$this->db->get();
);
$this->db->group_by('nID, nAID');
$query = $this->db->get();
答案 0 :(得分:0)
$this->db->select('nID, nAID');
$this->db->where('bValid', 1);
$this->db->where('nID', (int)$nID);
$this->db->from('event');
$this->db->group_by('nID, nAID');
转换为 从事件中选择nID,nAID,其中bValid = 1,nId ='$ nID'group by nID
您的子查询
$this->db->select('nID, nAID');
$this->db->where('bValid', 1);
$this->db->where('nID', (int)$nID);
$this->db->from(
$this->db->select('something, somethingelse');
$this->db->where('something', 1);
$this->db->get();
);
$this->db->group_by('nID, nAID');
大致翻译为
select nID, nAID select something,somethingelse where something=1 event where bValid=1 and nId='$nID' group by nID
我建议在循环中的每条记录上运行子查询
答案 1 :(得分:0)
第1步:将此代码放入DB_active_rec.php
// --------------------------------------------------------------------
/**
* Get SELECT query string
*
* Compiles a SELECT query string and returns the sql.
*
* @param string the table name to select from (optional)
* @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
* @return string
*/
public function get_compiled_select($table = '', $reset = TRUE)
{
if ($table !== '')
{
$this->_track_aliases($table);
$this->from($table);
}
$select = $this->_compile_select();
if ($reset === TRUE)
{
$this->_reset_select();
}
return $select;
}
第2步:试试这个
//Subquery
$this->db->select('something, somethingelse');
$this->db->where('something', 1);
$this->db->from('first_table');
$sub_query = $this->db->get_compiled_select();
$this->db->select('nID, nAID');
$this->db->where('bValid', 1);
$this->db->where('nID', (int)$nID);
$this->db->from("($sub_query) as tbl1");
$this->db->group_by('nID, nAID');
$query = $this->db->get();
告诉我它是否有效。