我已经使用以下查询从表中选择行。
Table 1:
id description status add_date topicid
1 xyz 0 22-3-13 5
2 pqr 0 21-3-13 5
3 abc 0 20-3-13 5
4 sdd 0 22-3-13 5
Table2:
id otherid
1 2
2 3
Run Code Online (Sandbox Code Playgroud)
该查询为我提供了table1中的所有记录,但我想选择不在table2中的那些记录。
像table1'id'不在table2'otherid'中。
在我的情况下,要从表1中选择ID为1和4的记录,因为它在表2中不作为“ otherid”出现。
$topicid = 5;
$q =$this->db->select(array(
't1.id as id',
't1.description',
't1.topicid',
't1.add_date'))
->from('table1 AS t1')
->where('t1.topicid',$topicid)
->where('t1.status',0)
->order_by('t1.add_date DESC)->get();
Run Code Online (Sandbox Code Playgroud)
尝试此查询将工作
$topicid = 5;
$q =$this->db->select(array(
't1.id as id',
't1.description',
't1.topicid',
't1.add_date'))
->from('table1 AS t1')
->where('t1.topicid',$topicid)
->where('t1.status',0)
->where('t1.id NOT IN (select otherid from table2)',NULL,FALSE)
->order_by('t1.add_date DESC)->get();
Run Code Online (Sandbox Code Playgroud)