表结构如下:
mysql> desc article;
+--------+--------------+------+-----+---------+----------------+
| Field  | Type         | Null | Key | Default | Extra          |
+--------+--------------+------+-----+---------+----------------+
| id     | int(11)      | NO   | PRI | NULL    | auto_increment |
| pid    | int(11)      | YES  |     | NULL    |                |
| rootid | int(11)      | YES  |     | NULL    |                |
| title  | varchar(255) | YES  |     | NULL    |                |
| cont   | text         | YES  |     | NULL    |                |
| pdate  | datetime     | YES  |     | NULL    |                |
| isleaf | int(11)      | YES  |     | NULL    |                |
+--------+--------------+------+-----+---------+----------------+ 如何找出title或者cont中带有“大象”且pid为0的记录?我以为用下面的语句就能搞定了:select * from article where title like '%大象%' or cont like '%大象%' and pid = 0;可惜……
那只是我以为!
感觉好像可以先找出pid=0的记录,然后再从中找出title或者cont中带有“大象”的记录,可就是不知道怎么下手啊,希望大家帮帮忙,在此提前表示感谢!

解决方案 »

  1.   

    select * from article where (title like '%大象%' or cont like '%大象%' ) and pid = 0;
      

  2.   

    select * from article 
    where (title like '%大象%' or cont like '%大象%') and pid = 0;
      

  3.   

    select * from article where pid = 0  and  title like '%大象%' or cont like '%大象%';
    这样试一下
      

  4.   

    >> 注意逻辑谓词的优先级顺序,从高到低:非, 与,或
    实在记不住,可以用括弧括住,就像楼上的几位说的那样
      

  5.   

    select * from article where (title like '%大象%' or cont like '%大象%') and pid = 0;