MySQL-快速入门(6)连接查询

1、内连接查询:inner join ... on

   只有满足条件的记录才能够出现在结果关系中,即完全相等。自连接查询是一种特殊的内连接查询。

2、外连接查询:

  1》左外连接 / 左连接:返回包括左表中的所有记录和右表中连接字段相等的记录。

  2》右外连接 / 右连接:返回包括右表中的所有记录和左表中连接字段相等的记录。

  3》全外连接:返回两个表中所有的数据记录。

3、子查询

  子查询常用的操作符有:any(some)、all、in、exists。

  1》any(some):

select * from t1 where t1.id > any (select id from t2 where t2.name=...)

  2》all:同上,可以与比较符一起使用。

  3》exists / not exists:

  4》in / not in:

  5》带比较运算符的子查询:<、<=、=、>=、!=(或<>)

  6》union / union all:合并查询结果

4、正则表达式查询

  MySQL使用regexp关键字指定正则表达式的字符匹配模式,默认不区分大小写。

  

  匹配指定字符串:只要这个字符串在查询文本中即可。如果要匹配多个字符串,多个字符串之间使用分隔符‘|’隔开。

select * from test where name regexp 'or|ap';

  匹配指定字符串中的任意一个:方括号“[]”指定一个字符集合,只匹配其中任何一个字符,即为所查找的文本。

select * from test where name regexp '[orap]';
select * from test where name regexp '[0-9]|[a-z]';  //[]中也可以是范围

  匹配指定字符以外的字符:

select * from test where name regexp '[^a-z0-9]';

  使用{n,}或者{n,m}来指定字符串连续出现的次数:

    {n,}:表示至少匹配n次前面的字符

    {n,m}:表示匹配前面的字符不少于n次,不多于m次。

select * from test where name regexp 'x{2,}';
或者
select * from test where name regexp 'x{2,4}';

5、数据的插入、更新、删除

   1》插入数据:

insert into tb_name(column_list) values (value_list);
或者
insert into tb_name(column_list) values (value_list),(value_list),...;
或者
insert into tb_name values (value_list),(value_list),...;
或者
insert into tb_name(column_list) select (column_list) from tb_name2 where ...;

   2》更新数据:

update tb_name 
   set column_name = value,...
 where ...;

   3》删除数据:

delete from tb_name [where ...];
truncate table tb_name;  //将直接删除原来的表,并重新创建一个表

猜你喜欢

转载自www.cnblogs.com/ZeroMZ/p/11456558.html