查看版本號: select version();
查看當前用戶:select user();
查看當前數據庫:show databases;
使用當前數據庫:use cms0308;
查看當前使用數據庫:select database();
查看當前數據表:show tables;
創建表:
creat table hack
(
id int,
username varchar(20),
password varchar(30)
);
create table news
(
id int,
title varchar(50)
);
向表中插入數據:
insert into hack values(1,'admin','123');
insert into hack values(2,'bose','234');
insert into news values(1,'web');
查詢表中的數據:
select * from hack;
select username password from hack;
查詢表中id=1的數據:
select * from hack where id=1;
通過exists函數判斷括號裏面的參數是否成立:
select * from news where id=1 and exists(select * from hack);即判斷是否存在hack表;
select * from news where id=1 and exists(select password from hack); 判斷hack表是否存在password字段;
通過order by排序:
select * from hack order by password;
select * from hack order by 2; 按查詢的結果的第二個字段排序;
select * from hack order by 3; 按查詢結果的第三個字段排序;
若是查詢結果的字段沒有那麼多的話 會返回錯誤;
通過order by判斷出查詢結果的字段可以使用union查詢:
UNION所有查詢的列數必須相同!
select * from news union select * from hack; 查詢不成功,因爲news表和hack表的字段不相同;
select * from news order by 2; 通過order by判斷news表的列數,最後判斷爲2列;
select * from news union select username, password from hack; 兩邊都是查詢兩列所以可以查詢,結果爲先顯示news表的數據,在顯示hack表的數據;
當hack表現查詢時怎麼辦(hack表列數爲3列,但是news表只有2列):
select * from hack union select 1,2,3 from news; (ACCESS中必須要表名)
select * from hack union select 1,2,3 (在MYSQL中可以不用表名)
MYSQL註釋:
單行註釋:#號,以“#”號開頭,直到該行行尾,全部是註釋內容;
-- :槓槓空格,以“-- ”開頭,直到該行行尾,全部是註釋內容;
多行註釋:/*......*/用於註釋文字的開頭,“*/”用於註釋文字的結尾。