一:数据库的创建以及表的操作

1、创建数据库

创建MySQL数据表需要以下信息:
● 表名
● 表字段名
● 定义每个表字段

create table xxx(
字段名1   数据类型,
字段名2   数据类型,
字段名1   数据类型
)

2、删除数据库/表

drop database <数据库名>;   //删除数据库
drop table xxx    //删除数据库表

3、查看数据库表的结构

3.1 修改表名

语法:
	alter table 旧表名 rename [TO] 新表名;
例如:
	alter table student rename people;
	ALTER table people rename student;

3.2 修改数据类型

语法:
	alter table 表名 modify 列名数据类型 约束条件;
例如:
	alter table student modify name char(3) not null;
	alter table student modify name varchar(20) ;
	
tips:
	当我们修改数据类型的时候,如果表里有数据,那么必须里面的数据符合你要修改的数据类型

3.3 修改列名

语法:
	alter table 表名 change 旧列名 新列名 数据类型;
例如:
	alter table student change name s_name varchar(20) not null;

3.4 删除列

语法:
	alter table 表名 drop 列名;
例如:
	alter table student drop s_name;
tips:
	删除列的话,那么列里的数据会跟着一起删除,无法还原。

3.5 添加列

语法:
	alter table 表名 add 新列名 数据类型 约束条件 [first | after 已存在的列名];
例如:
	alter table student add name varchar(20) not null DEFAULT '未命名';
	alter table student add birthday date after id;
tips:
	after添加的话,带表的是添加到哪个位置之后
	如果已经有数据,添加列的值会是默认值,没有设置默认值会是null

3.6 修改列的排列位置

语法:
	alter table 表名 modify 列名1 数据类型 约束条件 first | after 列名2;
例如:
	alter table student modify birthday date after name;

4、数据库的操作

desc 表名

show create talbe xxx查看数据库表的结构 (会给出创建表的语句)

insert into xxxx ( field1, field2,...fieldN ) values  ( value1, value2,...valueN );
或者
insert into xxxx ( ) values  ( value1, value2,...valueN );

delete  from table_name [WHERE Clause] 
如果没有指定 WHERE 子句,MySQL 表中的所有记录将被删除

update table_name set field1=new-value1, field2=new-value2 where  column_name="values"

select column_name,column_name from table_name [WHERE Clause] [LIMIT N][ OFFSET M]

4、MySQL对数据表进行分组查询(GROUP BY)

● 属性名:是指按照该字段的值进行分组。
● HAVING 条件表达式:用来限制分组后的显示,符合条件表达式的结果将被显示。
● WITH ROLLUP:将会在所有记录的最后加上一条记录。加上的这一条记录是上面所有记录的总和。

GROUP BY关键字通常与集合函数一起使用。集合函数包括COUNT()函数、SUM()函数、AVG()函数、MAX()函数和MIN()函数等。

● COUNT()函数:用于统计记录的条数。
● SUM()函数:用于计算字段的值的总和。
● AVG()函数:用于计算字段的值的平均值。
● MAX()函数:用于查询字段的最大值。
● MIN()函数:用于查询字段的最小值。

1、单独使用GROUP BY关键字进行分组

SELECT * FROM employee GROUP BY sex;

2、GROUP BY关键字与GROUP_CONCAT()函数一起使用

SELECT sex,GROUP_CONCAT(name) FROM employee GROUP BY sex;

3、GROUP BY关键字与集合函数一起使用

SELECT sex,COUNT(sex) FROM employee GROUP BY sex;

4、GROUP BY关键字与HAVING一起使用

SELECT sex,COUNT(sex) FROM employee GROUP BY sex HAVING COUNT(sex)>=3;

5、按照多个字段进行分组

SELECT * FROM employee GROUP BY d_id,sex;

6、GROUP BY关键字与WITH ROLLUP一起使用

使用WITH ROLLUP时,将会在所有记录的最后加上一条记录。这条记录是上面所有记录的总和。
实例:将employee表的sex字段进行分组查询。使用COUNT()函数计算每组的记录数,并且加上WITH ROLLUPSELECT语句的代码如下:
SELECT sex,COUNT(sex) FROM employee GROUP BY sex WITH ROLLUP;

7、 关于sql中join中sql 92 与 99语法的区别

在这里插入图片描述

内查询

在这里插入图片描述
sql 92语法

SELECT <select_list>
FROM Table_A A, Table_B B
ON A.Key = B.Key

sql 99语法

SELECT <select_list>
FROM Table_A A
INNER JOIN Table_B B
ON A.Key = B.Key
左查询

在这里插入图片描述
sql 92语法

SELECT <select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key

sql 99语法

SELECT <select_list>
FROM Table_A A,Table_B B
ON A.Key(+) = B.Key
右查询

在这里插入图片描述
sql 92语法

SELECT <select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key

sql 99语法

SELECT <select_list>
FROM Table_A A,Table_B B
ON A.Key = B.Key(+)
全查询

在这里插入图片描述
sql 92语法

SELECT <select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key  
union all
SELECT <select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key

sql 99语法

SELECT <select_list>
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.Key = B.Key
查询A部分

在这里插入图片描述

SELECT <select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
WHERE B.Key IS NULL
查询B部分

在这里插入图片描述

SELECT <select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key
WHERE A.Key IS NULL
查询除AB外的部分

在这里插入图片描述

SELECT <select_list>
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.Key = B.Key
WHERE A.Key IS NULL OR B.Key IS NULL

二:数据库查询经典练习题

一下列表来自于这个数据库–可以做练习用

1、表的创建以及数据的插入

CREATE TABLE customers
(
  c_id      int       NOT NULL AUTO_INCREMENT,
  c_name    char(50)  NOT NULL,
  c_address char(50)  NULL,
  c_city    char(50)  NULL,
  c_zip     char(10)  NULL,
  c_contact char(50)  NULL,
  c_email   char(255) NULL,
  PRIMARY KEY (c_id)
);

CREATE TABLE fruits
(
  f_id    char(10)      NOT NULL,
  s_id    INT		NOT NULL,
  f_name  char(255)     NOT NULL,
  f_price decimal(8,2)  NOT NULL,
  PRIMARY KEY(f_id)
) ;

CREATE TABLE suppliers
(
  s_id      int      NOT NULL AUTO_INCREMENT,
  s_name    char(50) NOT NULL,
  s_city    char(50) NULL,
  s_zip     char(10) NULL,
  s_call    CHAR(50) NOT NULL,
  PRIMARY KEY (s_id)
) ;

CREATE TABLE orderitems
(
  o_num      int          NOT NULL,
  o_item     int          NOT NULL,
  f_id       char(10)     NOT NULL,
  quantity   int          NOT NULL,
  item_price decimal(8,2) NOT NULL,
  PRIMARY KEY (o_num,o_item)
) ;

CREATE TABLE orders
(
  o_num  int      NOT NULL AUTO_INCREMENT,
  o_date datetime NOT NULL,
  c_id   int      NOT NULL,
  PRIMARY KEY (o_num)
) ;


/*--------------------------插入数据--------------------------*/
INSERT INTO customers(c_id, c_name, c_address, c_city, c_zip,  c_contact, c_email) 
VALUES(10001, 'RedHook', '200 Street ', 'Tianjin',  '300000',  'LiMing', 'LMing@163.com'),
(10002, 'Stars', '333 Fromage Lane', 'Dalian', '116000',  'Zhangbo','Jerry@hotmail.com'),
(10003, 'Netbhood', '1 Sunny Place', 'Qingdao',  '266000', 'LuoCong', NULL),
(10004, 'JOTO', '829 Riverside Drive', 'Haikou',  '570000',  'YangShan', 'sam@hotmail.com');


INSERT INTO fruits (f_id, s_id, f_name, f_price)
VALUES('a1', 101,'apple',5.2),
('b1',101,'blackberry', 10.2),
('bs1',102,'orange', 11.2),
('bs2',105,'melon',8.2),
('t1',102,'banana', 10.3),
('t2',102,'grape', 5.3),  
('o2',103,'coconut', 9.2),
('c0',101,'cherry', 3.2),
('a2',103, 'apricot',2.2),
('l2',104,'lemon', 6.4),
('b2',104,'berry', 7.6),
('m1',106,'mango', 15.6),
('m2',105,'xbabay', 2.6),
('t4',107,'xbababa', 3.6),
('m3',105,'xxtt', 11.6),
('b5',107,'xxxx', 3.6);


INSERT INTO suppliers(s_id, s_name,s_city,  s_zip, s_call)
VALUES(101,'FastFruit Inc.','Tianjin','300000','48075'),
(102,'LT Supplies','Chongqing','400000','44333'),
(103,'ACME','Shanghai','200000','90046'),
(104,'FNK Inc.','Zhongshan','528437','11111'),
(105,'Good Set','Taiyuang','030000', '22222'),
(106,'Just Eat Ours','Beijing','010', '45678'),
(107,'DK Inc.','Zhengzhou','450000', '33332');

INSERT INTO orderitems(o_num, o_item, f_id, quantity, item_price)
VALUES(30001, 1, 'a1', 10, 5.2),
(30001, 2, 'b2', 3, 7.6),
(30001, 3, 'bs1', 5, 11.2),
(30001, 4, 'bs2', 15, 9.2),
(30002, 1, 'b3', 2, 20.0),
(30003, 1, 'c0', 100, 10),
(30004, 1, 'o2', 50, 2.50),
(30005, 1, 'c0', 5, 10),
(30005, 2, 'b1', 10, 8.99),
(30005, 3, 'a2', 10, 2.2),
(30005, 4, 'm1', 5, 14.99);

INSERT INTO orders(o_num, o_date, c_id)
VALUES(30001, '2008-09-01', 10001),
(30002, '2008-09-12', 10003),
(30003, '2008-09-30', 10004),
(30004, '2008-10-03', 10005),
(30005, '2008-10-08', 10001);

查询练习

1.查询价格为10.2元的水果的名称,输入如下语句:

SELECT f_name, f_price FROM fruits  WHERE f_price = 10.2;

2.查找名称为“apple”的水果的价格,输入如下语句:

SELECT f_name, f_price FROM fruits WHERE f_name = 'apple';

3.查询价格小于10的水果的名称,输入如下语句:

SELECT f_name, f_price FROM fruits WHERE f_price < 10;

4.s_id为101和102的记录,输入如下语句:

SELECT s_id,f_name, f_price   FROM fruits WHERE s_id IN (101,102) ORDER BY f_name;

5.查询所有s_id不等于101也不等于102的记录,输入如下语句:

SELECT s_id,f_name, f_price
FROM fruits
WHERE s_id NOT IN (101,102)
ORDER BY f_name;

6.查询价格在2.00元到10.5元之间水果名称和价格

SELECT f_name, f_price FROM fruits WHERE f_price BETWEEN 2.00 AND 10.20;

7.查询价格在2.00元到10.5元之外的水果名称和价格

SELECT f_name, f_price FROM fruits WHERE f_price NOT BETWEEN 2.00 AND 10.20;

8.查找所有以‘b’字母开头的水果,输入如下语句:

SELECT f_id, f_name FROM fruits WHERE f_name LIKE 'b%';

9.在fruits表中,查询f_name中包含字母‘g’的记录

SELECT f_id, f_name FROM fruits WHERE f_name LIKE '%g%';

10.查询以‘b’开头,并以‘y’结尾的水果的名称

SELECT f_name FROM fruits WHERE f_name LIKE 'b%y';

11.在fruits表中,查询以字母‘y’结尾,且‘y’前面只有4个字母的记录

SELECT f_id, f_name FROM fruits WHERE f_name LIKE '____y';

12.查询customers表中c_email为空的记录的c_id、c_name和c_email字段值:

SELECT c_id, c_name,c_email FROM customers WHERE c_email IS NULL;

13.查询customers表中c_email不为空的记录的c_id、c_name和c_email字段值

SELECT c_id, c_name,c_email FROM customers WHERE c_email IS NOT NULL;

14.在fruits表中查询s_id = ‘101’,并且f_price大于5的记录价格和名称

SELECT f_id, f_price, f_name FROM fruits WHERE s_id = '101' AND f_price >=5;

15.在fruits表中查询s_id = ‘101’或者’102’,并且f_price大于5,并且f_name=’apple’的记录价格和名称

SELECT f_id, f_price, f_name FROM fruits 
WHERE s_id IN('101', '102') AND f_price >= 5 AND f_name = 'apple';

16.查询s_id=101或者s_id=102的水果供应商的f_price和f_name,SQL语句如下:

SELECT s_id,f_name, f_price FROM fruits WHERE s_id = 101 OR s_id = 102;

20.查询s_id=101或者s_id=102的水果供应商的f_price和f_name

SELECT s_id,f_name, f_price FROM fruits WHERE s_id IN(101,102);

21.查询fruits表中s_id字段的值,并返回s_id字段值不得重复

SELECT DISTINCT s_id FROM fruits;

22.查询fruits表的f_name字段值,并对其进行排序

select f_name from fruits ORDER BY f_name;

23.查询fruits表中的f_name和f_price字段,先按f_name排序,再按f_price排序

SELECT f_name, f_price FROM fruits ORDER BY f_name, f_price;

24.查询fruits表中的f_name和f_price字段,对结果按f_price降序方式排序

SELECT f_name, f_price FROM fruits ORDER BY f_price DESC;

25.查询fruits表,先按f_price降序排序,再按f_name字段升序排序,SQL语句如下:

SELECT f_price, f_name FROM fruits ORDER BY f_price DESC, f_name;

26.根据s_id对fruits表中的数据进行分组

SELECT s_id, COUNT(*) AS Total FROM fruits GROUP BY s_id;

27.根据s_id对fruits表中的数据进行分组,将每个供应商的水果名称显示出来****************

SELECT s_id, GROUP_CONCAT(f_name) AS Names FROM fruits GROUP BY s_id;

28.根据s_id对fruits表中的数据进行分组,并显示水果种类大于1的分组信息

SELECT s_id, GROUP_CONCAT(f_name) AS Names 
FROM fruits 
GROUP BY s_id HAVING COUNT(f_name) > 1;

29.根据s_id对fruits表中的数据进行分组(记录其中的个数),并显示记录数量

SELECT s_id, COUNT(*) AS Total 
FROM fruits 
GROUP BY s_id WITH ROLLUP;

30.根据s_id和f_name字段对fruits表中的数据进行分组, SQL语句如下,

SELECT * from fruits group by s_id,f_name;

31.查询订单价格大于100的订单号和总订单价格****************

SELECT o_num,  SUM(quantity * item_price) AS orderTotal
FROM orderitems
GROUP BY o_num
HAVING SUM(quantity*item_price) >= 100;

32.显示fruits表查询结果的前4行,输入如下语句:

SELECT * From fruits LIMIT 4;

33.在fruits 表中,使用LIMIT子句,返回从第5个记录开始的,行数长度为3的记录

SELECT * From fruits LIMIT 4, 3;

34.查询customers表中总的行数

SELECT COUNT(*) AS cust_num  from customers;

35.查询customers表中有电子邮箱的顾客的总数,输入如下语句:

SELECT COUNT(c_email) AS email_num FROM customers;

36.在orderitems表中,使用COUNT()函数统计不同订单号中订购的水果种类

SELECT o_num, COUNT(f_id) FROM orderitems  GROUP BY o_num;

37.在orderitems表中查询30005号订单一共购买的水果总量,输入如下语句:

SELECT SUM(quantity) AS items_total
FROM orderitems
WHERE o_num = 30005;

38.在orderitems表中,使用SUM()函数统计不同订单号中订购的水果总量

SELECT o_num, SUM(quantity) AS items_total
FROM orderitems
GROUP BY o_num;

39.在fruits表中,查询s_id=103的供应商的水果价格的平均值,SQL语句如下:

SELECT AVG(f_price) AS avg_price
FROM fruits
WHERE s_id = 103;

40.在fruits表中,查询每一个供应商的水果价格的平均值,SQL语句如下:

SELECT s_id,AVG(f_price) AS avg_price
FROM fruits
GROUP BY s_id;

41.在fruits表中查找市场上价格最高的水果,SQL语句如下:

mysql>SELECT MAX(f_price) AS max_price FROM fruits;

42.在fruits表中查找不同供应商提供的价格最高的水果

SELECT s_id, MAX(f_price) AS max_price
FROM fruits
GROUP BY s_id;

43.在fruits表中查找f_name的最大值,SQL语句如下

SELECT MAX(f_name) from fruits;

44.在fruits表中查找市场上价格最低的水果,SQL语句如下:

mysql>SELECT MIN(f_price) AS min_price FROM fruits;

45.在fruits表中查找不同供应商提供的价格最低的水果

SELECT s_id, MIN(f_price) AS min_price
FROM fruits
GROUP BY s_id;

46.在fruits表和suppliers表之间使用内连接查询,查询之前,查看两个表的结构,

SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits ,suppliers
WHERE fruits.s_id = suppliers.s_id;

47.在fruits表和suppliers表之间使用INNER JOIN语法进行内连接查询

SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits INNER JOIN suppliers
ON fruits.s_id = suppliers.s_id;

48.查询供应f_id=’a1’的水果供应商提供的其他水果种类

SELECT f1.f_id, f1.f_name
FROM fruits AS f1, fruits AS f2
WHERE f1.s_id = f2.s_id AND f2.f_id = 'a1';

49.在customers表和orders表中,查询所有客户,包括没有订单的客户,SQL语法如下,

SELECT customers.c_id, orders.o_num
FROM customers LEFT OUTER JOIN orders
ON customers.c_id = orders.c_id;

50.在customers表和orders表中,查询所有订单,包括没有客户的订单

SELECT customers.c_id, orders.o_num
from customers RIGHT OUTER JOIN orders
ON customers.c_id = orders.c_id;

51.在customers表和orders表中,使用INNER JOIN语法查询customers表中ID为10001、的客户的订单信息

SELECT customers.c_id, orders.o_num
FROM customers INNER JOIN orders
ON customers.c_id = orders.c_id AND customers.c_id = 10001;

52.在fruits表和suppliers表之间使用INNER JOIN语法进行内连接查询,并对查询结果排序

 SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits INNER JOIN suppliers
ON fruits.s_id = suppliers.s_id
ORDER BY fruits.s_id;

定义两个表tb1和tb2

CREATE table tbl1 ( num1 INT NOT NULL);
CREATE table tbl2 ( num2 INT NOT NULL);
向两个表中插入数据,
INSERT INTO tbl1 values(1), (5), (13), (27);
INSERT INTO tbl2 values(6), (14), (11), (20);

53.返回tbl2表的所有 num2 列,然后将 tbl1 中的 num1 的值与之进行比较,只要大于 num2的任何值为符合查询条件的结果。

SELECT num1 FROM tbl1 WHERE num1 > ANY (SELECT num2 FROM tbl2);

54.返回tbl1表的中比tbl2表num2 列所有值都大的值

SELECT num1 FROM tbl1 WHERE num1 > ALL (SELECT num2 FROM tbl2);

55.查询表suppliers表中是否存在s_id=107的供应商,如果存在则查询fruits表中的记录

SELECT * from fruits
WHERE EXISTS
(SELECT s_name FROM suppliers WHERE s_id = 107);

56.查询表suppliers表中是否存在s_id=107的供应商,如果存在则查询fruits表中的f_price大于10.20的记录

SELECT * from fruits
WHERE f_price>10.20 AND EXISTS
(SELECT s_name FROM suppliers WHERE s_id = 107);

57.查询表suppliers表中是否存在s_id=107的供应商,如果不存在则查询fruits表中的记录

SELECT * from fruits
WHERE NOT EXISTS
(SELECT s_name FROM suppliers WHERE s_id = 107);

58.在orderitems表中查询订购f_id为c0的订单号,并根据订单号查询具有订单号的客户c_id

SELECT c_id FROM orders WHERE o_num IN
(SELECT o_num  FROM orderitems WHERE f_id = 'c0');

59.与前一个例子语句类似,但是在SELECT语句中使用NOT IN操作符

SELECT c_id FROM orders WHERE o_num NOT IN
(SELECT o_num  FROM orderitems WHERE f_id = 'c0');

60.在suppliers表中查询s_city等于Tianjin的供应商s_id,然后在fruits表中查询所有该供应商提供的水果的种类

SELECT s_id, f_name FROM fruits
WHERE s_id =
(SELECT s1.s_id from suppliers AS s1 WHERE s1.s_city = 'Tianjin');

61.在suppliers表中查询s_city等于Tianjin的供应商s_id,然后在fruits表中查询所有非该供应商提供的水果的种类,SQL语句如下,

SELECT s_id, f_name FROM fruits
WHERE s_id <>
(SELECT s1.s_id from suppliers AS s1 WHERE s1.s_city = 'Tianjin');

62.查询所有价格小于9的水果的列表,查询s_id等于101和103所有的水果种类,使用UNION连接查询结果

SELECT s_id, f_name, f_price 
FROM fruits
WHERE f_price < 9.0
UNION ALL
SELECT s_id, f_name, f_price 
FROM fruits
WHERE s_id IN(101,103);

63.查询所有价格小于9的水果的列表,查询s_id等于101和103所有的水果种类,使用UNION ALL连接查询结果,SQL语句如下

SELECT s_id, f_name, f_price 
FROM fruits
WHERE f_price < 9.0
UNION ALL
SELECT s_id, f_name, f_price 
FROM fruits
WHERE s_id IN(101,103);

64.为orders表取别名o,查询订30001订单的下单日期

SELECT * from orders AS o 
WHERE o.o_num = 30001;

65.为customers和orders表分别取别名,并进行连接查询

SELECT c.c_id, o.o_num
FROM customers AS c LEFT OUTER JOIN orders AS o
ON c.c_id = o.c_id;

66.查询fruits表,为f_name取别名fruit_name,f_price取别名fruit_price,为fruits表取别名f1,查询表中f_price < 8的水果的名称

SELECT f1.f_name AS fruit_name, f1.f_price AS fruit_price
FROM fruits AS f1
WHERE f1.f_price < 8;

67.查询suppliers表中字段s_name和s_city,使用CONCAT函数连接这个两个字段值,并取列别名为suppliers_title。

如果没有对连接后的值取别名,其显示列名称将会不够直观,输入如下SQL,

SELECT CONCAT(RTRIM(s_name) , ' (',  RTRIM(s_city), ')')
FROM suppliers
ORDER BY s_name;

68.在fruits表中,查询f_name字段以字母‘b’开头的记录

SELECT * FROM fruits WHERE f_name REGEXP '^b';

69.在fruits表中,查询f_name字段以“be”开头的记录

SELECT * FROM fruits WHERE f_name REGEXP '^be';

70.在fruits表中,查询f_name字段以字母‘t’结尾的记录

SELECT * FROM fruits WHERE f_name REGEXP 'y$';

71.在fruits表中,查询f_name字段以字符串“rry”结尾的记录

SELECT * FROM fruits WHERE f_name REGEXP 'rry$';

72.在fruits表中,查询f_name字段值包含字母‘a’与‘g’且两个字母之间只有一个字母的记录

SELECT * FROM fruits WHERE f_name REGEXP 'a.g';

73.在fruits表中,查询f_name字段值以字母‘b’开头,且‘b’后面出现字母‘a’的记录

SELECT * FROM fruits WHERE f_name REGEXP '^ba*';

74.在fruits表中,查询f_name字段值以字母‘b’开头,且‘b’后面出现字母‘a’至少一次的记录

SELECT * FROM fruits WHERE f_name REGEXP '^ba+';

75.在fruits表中,查询f_name字段值包含字符串“on”的记录

 SELECT * FROM fruits WHERE f_name REGEXP 'on';

76.在fruits表中,查询f_name字段值包含字符串“on”或者“ap”的记录

 SELECT * FROM fruits WHERE f_name REGEXP 'on|ap';

77.在fruits表中,使用LIKE运算符查询f_name字段值为“on”的记录

SELECT * FROM fruits WHERE f_name LIKE 'on';

78.在fruits表中,查找f_name字段中包含字母o或者t的记录

SELECT * FROM fruits WHERE f_name REGEXP '[ot]';

79.在fruits表,查询s_id字段中数值中包含4、5或者6的记录

SELECT * FROM fruits WHERE s_id REGEXP '[456]';

80.在fruits表中,查询f_id字段包含字母a到e和数字1到2以外的字符的记录

SELECT * FROM fruits WHERE f_id REGEXP '[^a-e1-2]';

81.在fruits表中,查询f_name字段值出现字符串‘x’至少2次的记录

 SELECT * FROM fruits WHERE f_name REGEXP 'x{2,}';

82.在fruits表中,查询f_name字段值出现字符串“ba”最少1次,最多3次的记录

SELECT * FROM fruits WHERE f_name REGEXP 'ba{1,3}';
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐