1.所有的表的连接都有where连接,但是可以使用其他的进行代替
等值连接,基于两个表之间的相等测试,这种连接也称为内部连接。
使用inner join... on ... 其中inner join连接两个表,使用on和where的条件相同
select * from user inner join student on user.sid = student.sid
2.使用外连接,使用outer join,这样必须使用left ,right等配合使用。比如使用right outer join那么就表明包含右边的表的所有数据。也就是外部连接不仅仅包含两个表的相关联的行,还包含没有关联的行。
select * from user right outer join student on user.sid = student.sid
3.使用union关键字,这样能将两次查询后的数据,合并到一起进行返回,但是需要注意的是,两次查询的字段必须能兼容。并且会自动过滤重复的数据。如果想保留重复的数据, 那么就使用union all关键字。并且可以使用union进行连接多个查询语句。
select name from user union select name from student
4.使用全文本搜索 ,比like和regexp更加高效智能。
create table teacher (tid int primary key auto_increment not null,
name varchar(20) not null,
address varchar(20) not null,
sid int ,
fulltext(address)
)engine=myisam
必须使用match() 和against(),分别制定开启全局搜索的字段和內容,注意,如果是字段是3个和以下的字符的词,自动就被忽略,但是可以进行修改。如果行数少于三行就默认不显示。
select address from teacher where match(address) against("lalala")
可以使用布尔文本搜索,这是全局搜索的另一种形式。这样及时没有使用fulltext也能使用
select address from teacher where match(address) against("lalala" in boolean mode)
在里面可以使用一些全文本布尔操作符
+ 必须包含
- 必须排除
> 包含 而且增加等级值
< 包含 而且减少等级值
* 词尾通配符
表示没有以rope开头的词
select address from teacher where match(address) against("lalala -rope*" in boolean mode)