[b]quote:[/b]
create table orders (
id int not null auto_increment,
name varchar(100) not null,
email varchar(255) not null,
address text not null,
pay_type char(10) not null,
shipped_at datetime null,
primary key (id));
首先,将要匹配相同条件的字段连起来(field1+field2+...)成一个长字符串;然后再 Like “%cond%”就可以了。一般来说,单表内字段连接后再统一like判断;表间字段,则需要先过滤后,再实行这个策略。采取这个策略,不仅可以缩短SQL,而且能够有效地提高SQL的执行效率。
1. 对于两个field,同一关键字模糊查询
select id,name,email from orders where concat(name,email) like '%aa%'
2. 对于两个以上field,用||
select id,name,email from orders where lower(name||email||address) like '%aa%'
3. 多字段多关键字查询
select id,name,email from orders where concat(name,email) like '%aa%bb%'
create table orders (
id int not null auto_increment,
name varchar(100) not null,
email varchar(255) not null,
address text not null,
pay_type char(10) not null,
shipped_at datetime null,
primary key (id));
首先,将要匹配相同条件的字段连起来(field1+field2+...)成一个长字符串;然后再 Like “%cond%”就可以了。一般来说,单表内字段连接后再统一like判断;表间字段,则需要先过滤后,再实行这个策略。采取这个策略,不仅可以缩短SQL,而且能够有效地提高SQL的执行效率。
1. 对于两个field,同一关键字模糊查询
select id,name,email from orders where concat(name,email) like '%aa%'
2. 对于两个以上field,用||
select id,name,email from orders where lower(name||email||address) like '%aa%'
3. 多字段多关键字查询
select id,name,email from orders where concat(name,email) like '%aa%bb%'