sql 如何选择与"或在哪里"连接的强制性位置?



我需要进行以下选择:

select * from posts where active = 1 //mandatory where url <> 'thisurl' //mandatory or where id1 = 1 or where id2 = 2

明白了吗?此外,我正在使用Codeigniter active record,所以如果有人能帮忙的话感谢我翻译这篇文章。

在Codeigniter查询生成器语言中:

$this->db->select('*');
$this->db->where('active', 1);
$this->db->where('url !=', 'thisurl');
$this->db->group_start();
$this->db->or_where('id1', 1);
$this->db->or_where('id2', 2);
$this->db->group_end();
$query = $this->db->get();
return $query->result();

以上内容将始终强制执行active=1和url<>'thisurl',并强制执行至少id1=1或id2=2

试试下面的

select * from posts where active = 1       //mandatory
and url <> 'thisurl' //mandatory
and ( id1 =1 or id2 =2)

您可以执行此

SELECT* 
FROM posts 
WHERE active = 1       //
AND url <> 'thisurl' //mandatory
OR id1 = 1 
OR id2 = 2

不要忘记括号!

SELECT * from posts 
WHERE (active = 1 AND url <> 'thisurl') OR id1 = 1 OR id2 = 2

您可以使用$where来简化where查询

$this->db->select('*');
$this->db->from('posts');
$where ="(
active = 1       //mandatory
where url <> 'thisurl' //mandatory
or where id1 = 1 
or where id2 = 2
)";
$this->db->where($where);
$result = $this->db->get();

相关内容

  • 没有找到相关文章

最新更新