一: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%';