当前位置:Gxlcms > mysql > mysql查询语句基本用法_MySQL

mysql查询语句基本用法_MySQL

时间:2021-07-01 10:21:17 帮助过:39人阅读

一:mysql基本语句

1.显示可用数据库

show databases;

2.创建一个library数据库

create databaselibrary;

3.使用数据库

use library;

4.查看表

show tables;

5.建表语句

mysql> uselibrary;

Database changed

mysql> createtable viedos(

-> video_id int(11) not nullauto_increment,

-> title varchar(255) not null,

-> director varchar(255) not null,

-> primary key(video_id));

Query OK, 0 rowsaffected (0.30 sec)

6.插入数据

insert intoviedos(video_id,title,director)values

(1,'大上海','武打片'), (2,'小上海','爱情片'),(3,'红上海','喜剧片'),(4,'黑上海','枪打片'),(5,'大上海','散打片');

7.删除表

delete from table_name;

8.更新表数据

update viedos settitle='上海滩' where video_id=1;

二:执行查询语句

A:基本语句

1.查询全部

SELECT * FROMviedos;

2.查询部分

SELECT title FROMviedos;

3.去掉查询结果相同的部分

SELECT distinct title FROM viedos;

SELECT title FROM viedosWHERE director='武打片';

B:使用逻辑比较运算符

创建一张新表:

CREATE TABLE core(

sname VARCHAR(25)NOT NULL,

math INT NOT NULL,

physics INT NOTNULL,

literature INT NOTNULL)

插入数据

INSERT INTO coreVALUES('john',68,37,45),('jim',96,89,92),

('bill',65,12,45),('harry',68,25,82);

查询1:

SELECT * FROM coreWHERE math>90;

查询2:

SELECT sname FROMcore WHERE math>85;

查询3:

SELECT * FROM coreWHERE math<=25 OR physics<=25 OR literature<=25;

查询4:

SELECTsname,math+physics+literature FROM core;

C:使用函数查询

查询1:

SELECT COUNT(*)FROM core;

查询2:

SELECT AVG(math)FROM core;

查询3:

SELECTMIN(math),MAX(physics) FROM core;

D:筛选查询结果

查询1:降序

SELECT math FROMcore ORDER BY math DESC;

查询2:显示两条记录

SELECT math FROMcore LIMIT 2,2;

查询3:联合使用

SELECT math FROMcore ORDER BY math DESC LIMIT 2,2;

E:使用通配符查询

查询1:

SELECT sname FROMcore WHERE sname LIKE '%j%';

查询2:

SELECT math FROMcore WHERE math LIKE '%6%';

人气教程排行