求一句SQL语句.SQL高手进~~~~~

来源:百度知道 编辑:UC知道 时间:2024/05/31 00:19:52
现有一个list表
表里面有title和content两个字段
我想搜索title或content里面有"你好"两个字的记录.
不管是title字段里面有还是content里面有"你好"这两个字.都要列出来.
效率高的SQL语句应该怎么写?

就用我这个最好 2楼的用union的效率极差不知道吗?如果数据量大的话就慢死了
select * from list
where title like '%你好%'
or content like '%你好%';

select title,content from list where instr(title,'你好') <>0
union
select title,content from list where instr(content,'你好') <>0

通常情况下UNION要比OR的效率高(OR两边的字段相同的情况下除外).所以这里还是用UNION较好

使用LIKE时,如果字符串的左边没有'%',且建立了索引的话,则效率会高,否则不然。举个例子:
如:name like ‘张%’,属于SARG,效率高。
而:name like ‘%张’ ,就不属于SARG。
原因是通配符%在字符串的开头使得索引无法使用。

select 字段1,字段2,字段3,... from [list] where title like '%你好%'
union
select 字段1,字段2,字段3,... from [list] where content like '%你好%'

select * from list where title like '%你好%' or content like '%你好%'