數據操作與查詢語句

1數據操作語句:

     新增:Insert into 表名(列名1, 列名2, 列名3...) values (列名1值,列名2值, 列名3值.)

                           兩種新增數據的方式

                          Insert into stu(sid,sname,sage)values(1,’李林’,22);

                          Insert into stu values(1,’李林’,22);

               修改Update 表名 set 列名1=修改的值,列名2=修改的值;

                         update stu SET sage=23,sname='李琳'

               刪除:Delete from 表名

                         Delete from stu;  

2數據查詢語句:

              查詢全部數據:

                               Select * from 表名;

                               Select * from stu;

              根據條件查詢指定的數據:

                              Select * from 表名 where 列名1=值 and 列名2=值....

                              Select * from stu where sid=9 and ssex='女';

 

              查詢數據,返回指定的列:

                              Select 列名1,列名2 from stu;

                              Select sid,sname from stu;

 

             給指定返回列取別名(小名):

                       兩種方式:

                              Select 列名 別名,列名2 別名2... from 表名;

                              Select 列名 as 別名,列名2 as 別名2... from 表名;

 

                              Select sid 學號,sname 姓名,ssex 性別 from stu;

                              Select sid as  學號,sname as 姓名,ssex as 性別 from stu;

 

      在條件中使用比較運算符:

                              SELECT * FROM 表名 where 字段 > < >= <= !=或<>

                              select * from j18 where xsnianling !=18

 

             多條件的查詢

                              AND OR NOT

                              select * from j18 where xsnianling <=21 and xsxingbie='女'

                              select * from j18 where xsnianling <21 or xsxingbie='女'

                              select * from j18 where xsnianling not in(18,21,25)

             對空值的查詢:

                            is null  對應列是否null查詢

                           select * from j18 where xsxueli is not null

                           select * from j18 where xsxueli is null

             BETWEEN A AND B在A和B之間,包含AB的值

                           select * from j18 where xsnianling BETWEEN 18 and 21

 

             IN:

                           select * from j18 where xsnianling in(18,21,25)

     

      模糊查詢 LIKE:

                           %:指代不明確值的位置或長度

                           _:指代明確值的位置或已知字符串長度

                           select * from j18 where xsxingming like '_靈%'

 

      查詢中使用算術表達式:+ - * /

                           select xsxuehao+xsnianling from j18 where xsxingming like '_靈%'

 

             處理重複值:DISTINCT   排除重複展示,只展示一次

                           select DISTINCT xsxingbie from j18;

 

             查詢返回限定行數:LIMIT

                           Limit 10 取查詢數據的前10位

                           Limit 10,10  從查詢數據的第11位開始,向後取10位數據展示,不滿足10位也不會報錯

 

              通過查詢複製表

                           create table stu1  select * from stu;

 

               --只複製結構

                           create table stu2  select * from stu where 1=2;

      分組 group by

                           select ssex,COUNT(*) from stu GROUP BY ssex

                            分組使用的時候,,group by 字段,一定要在 select  後面出現,如果使用了group by                                     select 後面就不要出現 *

 

     排序 order by 字段名 字段名就是我們需要排序的字段

                           order by  xsnianling  升序  默認

                           order by  xsnianling desc  降序

     聚合函數:

                          COUNT  統計數量:select count(xsnianling) from j18

                          SUM    求和:select sum(xsnianling) from j18

                          MAX    求最大值:select max(xsnianling) from j18

                          MIN    求最小值:select min(xsnianling) from j18

                          AVG    平均數:select avg(xsnianling) from j18


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章