mysql 查詢

# 查詢
  # select * from 表名python

# 消除重複行
  # 在select後面列前使用distinct能夠消除重複行
    # select distinct gender from students;數組


  # select * from 表名 where
  # 比較運算符
    # 等於 =
    # 大於 >
    # 大於等於 >=
    # 小於 <
    # 小於等於 <=
    # 不等於 !=或<>函數

      # 查詢科目不大於4的科目
        # select * from subjects where id<=4ui

      # 查詢姓名不是黃蓉的學生
        # select * from students where name!='黃蓉'rest

      # 查詢沒有被刪除的姓名
        # select * from students where isDelete=0;排序

    # 邏輯運算符
    # and
    # or
    # not索引

      # 查詢大於三的女同窗
        # select * from students where id>3 and gender=0; 事務

      # 查詢編號小於4或沒有被刪除的同窗
        # select * from students where id<4 or isDelete=0;ci

    # 模糊查詢
    # like
    # %表示任意多個字符
    # _表示一個任意字符rem

      # 查詢姓黃的學生
        # select * from students where name like '黃%';
        # select * from students where name like '黃_';
      # 查詢姓黃的或者帶靖的學生
        # select * from students where name like '黃%' or name like '%靖%';

    # 範圍查詢
    # in表示在一個非連續的範圍內

      # 查詢編號是1或者3或者8的學生
        # select * from students where id in(1,3,8);

      # 查詢編號3到8的學生
        # select * from students where id between 3 and 8;

      # 查詢編號是3到8的男生
        # select * from students where id between 3 and 8 and gender=1;


    # 空判斷
    # null與''是不一樣的
    # 判空 is null
    # 判斷非空 is not null

      # 生日是空的
        # select * from students where birthday is null;

      # 生日不爲是空的女生
        # select * from students where birthday is not null and gender=0;


  # 優先級
    # 小括號,not 比較運算符,邏輯運算符
    # and比or先運算,若是同時出現並但願先算or,須要結合()使用

 

# 聚合
  # 爲了快速獲得統計數據,提供5個聚合函數

  # count(*)表示計算總行數
    # 查詢學生總數
      # select count(*) from students where isDelete=0;

  # max(列)表示求此列的最大值
    # 查詢女生的編號最大值
      # select max(id) from students where gender=0;

  # min(列)表示求此列的最小值
    # 查詢未刪除編號最小值
      # select min(id) from students where isDelete=0;
      # select * from students where id=(select min(id) from students where isDelete=0);

  # sum(列)表示求此列的和
    # 查詢男生的和
      # select sum(id) from students where gender=1;


  # avg(列)表示求此列的平均值
    # 查詢未刪除女生的平均值
      # select avg(id) from students where isDelete=0;


# 分組
  # 按照字段分組,表示此字段相同的數據會被放到一個數組中
    # select 列1,列2,聚合 from group by 列1,列2
      # 查詢男女生總數
        # select gender as 性別,count(*) from students group by gender;

  # 數據篩選
    # having... 對分組後結果集篩選。
      # 查詢男生總數
        # select gender,count(*) as re from students group by gender having gender=0;

      # 對比where與having
        # where是對from後面指定的表進行數據篩選,屬於對原始集數據的篩選
        # having是對group by的結果進行篩選。


# 排序
  # 爲了方便產看數據,能夠對數據進行排序
  # 語法:
    # select * from 表名
    # order by列1 asc(有小到大)|desc(由大到小),列2 asc(有小到大)|desc
    # 默認按照列值由小到大

      # 查詢未刪除的男生由大到小
        # select * from students where isDelete=0 and gender=1 order by id desc;
      # 查詢未刪除科目信息,按名稱升序
        # select * from subjects where isDelete=0 order by id asc;


# 分頁
  # 當數據量過大時,在一頁中查看數據是一件麻煩的事情
  # select * from 表名
  # limit start,count

  # 從start開始,獲取count條數據
  # start索引從0開始

    # 已知:每頁顯示m條數據,當前顯示第幾頁
    # 求總頁數,此邏輯會在後面的python中實現、
    # 查詢總條數p1
    # 使用p1除以m達到p2
    # 若是整除則p2爲總數頁
    # 若是不整除則p2+1爲總頁數

    # limit start,m

    # m = 5

    # n start
    # 1 0
    # 2 m
    # 3 2m
    # 4 3m
    # n (n-1)m

      # select * from students where isDelete=0 limit (n-1)*m,m

  # 執行順序
    # from 表名
    # where...
    # group by...
    # select distinct *
    # having...
    # order by...
    # limit start,count

 

完整的select語句

  select distinct * 

  from  表名

  where ......

  group by ... having...

  order by ...

  limit start,count

 

# 實體與實體之間有3種對應關係,這些關係也須要存儲下來
  # 關係
    # 1.試圖用於完後曾查詢語句的封裝
    # 2.事務能夠保證複雜的增刪改查操做有效
    # 3.當數據巨大時,爲了提升查詢速度能夠經過索引實現


# 建立scores,結構以下:
  # id
  # score
  # 學生
  # 科目
  # create table scores(
    # id int primary key auto_increment not null,
    # score decimal(5,2),
    # stuid int,
    # subid int,
    # foreign key(stuid) references students(id),
    # foreign key(subid) references subjects(id)
  # )


  # 外鍵的級聯操做

    # 級聯操做的類型包括:
      # restrict(限制):默認值,拋異常
      # cascade(級聯):若是主表的記錄刪掉了,則從表種相關的數據都將被刪除
      # set null: 將外鍵設置爲空
      # no action: 什麼都不作

  # 鏈接
    # 連接查詢
      # 查詢學生郭靖python科目的成績
        # select students.name,subjects.title,scores.score from scores inner join students on scores.stuid=students.id inner join subjects on scores.subid=subjects.id;

        # select students.name,subjects.title,scores.score from students inner join scores on scores.stuid=students.id inner join subjects on scores.subid=subjects.id;


    # 連接查詢3種
      # 表A inner join 表b; 表A與表B匹配的
      # 表A left join 表b; 以左表的信息爲準,外加表A中獨有的數據,未對應的數據使用null填充
      # 表A right join 表b; 以右表的信息爲準,外加表B中獨有的數據,未對應的數據使用null填充

      # select name,avg(score) from scores
      # inner join students on scores.stuid=students.id
      # group by stuid;

 

# select distinct 列*# from inner|left|right join 表2 on 表1與表2的關係# where 比較運算符(>,>=,<,<=,=,!=,<>),邏輯運算符(and,or,not),空判斷,模糊查詢(like)# group by ... (聚合) 屬性 having 屬性=值.# order by ... (排序) asc | desc# limit start,count

相關文章
相關標籤/搜索