问题描述
- sql语句的逻辑问题。。。
-
语句出错了,如下:
$_sql = "SELECT COUNT(*) FROM cms_content c,cms_nav n WHERE c.nav=n.id AND c.nav IN (SELECT id FROM cms_nav WHERE pid='$this->nav') OR c.nav='$this->nav'";模型就是
SELECT * FROM tablename WHERE 条件1 AND 条件2 OR 条件3;现在逻辑关系应该是(1 and 2) or 3;
我想要 1 and (2 or 3),请问怎样才能实现呢? (我用括号或花括号框上2、3都不行。。)
解决方案
SELECT * FROM tablename WHERE 条件1 AND 条件2 OR 条件1 AND 条件3;
如果条件1 是必须的话 就是这样。
解决方案二:
试试 SELECT * FROM tablename WHERE (条件1 AND 条件2) OR ((条件1 AND 条件3);
解决方案三:
SELECT * FROM tablename WHERE (条件1 AND 条件2) OR (条件1 AND 条件3)
解决方案四:
SELECT * FROM tablename WHERE (条件1 AND 条件2) OR (条件1 AND 条件3)
解决方案五:
$_sql = "SELECT COUNT(*) FROM cms_content c,cms_nav n WHERE c.nav=n.id AND (c.nav IN (SELECT id FROM cms_nav WHERE pid='$this->nav') OR c.nav='$this->nav')";
解决方案六:
SELECT COUNT(* ) FROM cms_content c,cms_nav n
WHERE (c.nav=n.id AND c.nav IN (SELECT id FROM cms_nav WHERE pid='$this->nav') )
OR (c.nav=n.id AND c.nav='$this->nav')