數據庫學習筆記——基本查詢語句

數據庫基本操作:

  • select 要查詢的列 from 表名 where 查詢條件
  • select語句:查詢
select name from product
單獨檢索產品表中產品名稱這一列

select * from product
檢索產品表中所有列

限制結果:

  • distinct語句:去重
select distinct name from product
檢索產品表中所有產品名稱,並且去掉名稱重複的數據
  • limit語句:限制讀取n條記錄
select name from product limit 4;
讀取產品表中的產品名稱,且只取前4條記錄

select name from product limit 2,5;
讀取產品表中的產品名稱,且只從第3條記錄開始讀取,讀取5條記錄
  • 表名.列名字:限制使用字段的表名
select produt.id from product
product.id,表示讀取產品表中的id列
  • order by 字段:針對某個字段排序
  • order by字段:針對某些字段排序,字段位置很重要,優先根據靠前的字段排序
select id,name from product order by name;
select id,name from product order by id;
--可針對id 或者名稱都可以排序

select id,name from product order by name,id;
優先根據產品名稱進行排序,若名稱相同,則根據id排序
  • asce:按照升序排序(可不寫,默認爲升序)
  • desc:按照降序排序
select id,name from product order by name desc,id asce;
在產品列表中,將現將名字按照降序排序,後id按照升序排序(先按照名稱排序,名稱排序相同時,在按照id的升序排序)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章