MySQL第五天(sql語句練習)

1.select * from tablename;返回給定數據表的全部數據記錄mysql

2.selece now();返回當前時間sql

3.select count(id) from tablename;統計一個數據表中有多少列。mysiam引擎記錄了表中有多少行。innodb卻沒有。數據庫

4.select count(distinct name) from tablename;按照name列統計,統計表中有多少列,distinct表示去除重複的。函數

5.select id from tablename;查詢表中的某一列信息,不會去除重複的內容。3d

6.select name from tablename limit 2;限制返回兩條數據,相比不限制返回,節約了cpu,帶寬。orm

7.select name from tablename limit 2,2;返回第三條和第四條數據。這裏的前一個2表示偏移量,後一個2表示在返回多少條數據。blog

8.select sql_calc_found_rows name from tablename limit 2;sql_calc_found_rows使得下一次使用select found_rows();時會返回此次查詢記錄的總數,也就是name的總條數。上面的語句只能返回兩條記錄。排序

9.select found_rows();返回上次查詢時記錄的總數。索引

10.select name from tablenae order by name [desc];使查詢結果顯示按照name列定義的排序方式排列,desc可選使排序結果反轉。ip

11.數據的排序方式的選擇,1.看定義的數據列排序方式。2數據表的排序方式。3數據庫的排序方式。4那個數據列的字符集排序方式。給定默認字符集的排序方式能夠用 show character set查看。

12.select name from my_student order by name collate 排序方式;臨時改變數據列的排序方式,不會使用到索引,因此排序很慢。

13.select name from my_student where name>='a';對查詢結果賽選,name>='a'即名字中首字母大於a的名字,uft排序b>a;

14.select name from my_student where name like '%big%';使用通配符like進行篩選。%符號表示任意字符。

15.select name form my_studnet where id in (1,2);使用枚舉型in來進行篩選,id爲1或者2的就顯示。

16.mysql不支持 column=null這樣的語法。若是須要對包含null的數據記錄進行搜索就必需要使用到函數isnull(colmum);

17.select * from table1 [left|right|inner] join table2 on table1.id=table2.id;使用left join就將以table1爲基礎表顯示所有信息,table2表只返回的信息符合後邊條件的信息。right join相似,以右表table2爲基礎。而inner則是返回兩個表都知足條件的記錄。

18.select name from my_student where id>=7 union [all]select name from my_student where id<=5;聯合多條select命令執行,結果一併返回,可選項all用於顯示重複的內容,默認mysql返回結果消除了重複的。tips:mysql中存放的數據不區分大小寫。

19.(select name from my_student where id>=7 )union [all](select name from my_student where id<=5)order by name limit 2;使用圓括號可讓返回的結果使用order by和limit語法。

20.select name ,pname from my_student,my_profile where my_profile.pname=my_student.name;此句sql命令和以前的inner join命令差很少。

21.select name, count(id) as 'id計算' from my_student group by name;group by用於分組,和聚合函數一塊兒使用,這裏將name相同的進行id統計,name名相同的消除。as至關於給count(id)起了一個別名。

22.

23.對多個數據列進行group by查詢。

24.若是group by字段只有一個數據列,加上 with rollup 將會有一條統計記錄。

25.若是group by字段有多個個數據列,加上 with rollup 將會在每一條記錄的後邊生成一個小計,最後再合計。

26.爲數據表建立一個備份數據表,可是新數據表裏邊沒有任何索引

27.delete from tablename-刪除一個數據表中的所有內容,drop table tablename-刪除一個數據表,insert into tablename select * from tablename-從一個數據表中恢復數據到另外一個數據表。

28.爲整個數據庫建立備份。

29.重一個備份文件中回覆數據。

首先必需要有一個數據庫-create database databasename;use databasename;source backstudent;這樣新建的數據庫中就有備份的數據了

相關文章
相關標籤/搜索