1. 程式人生 > >簡單的SQL語句的增、刪、改、查

簡單的SQL語句的增、刪、改、查

        1、增: insert into 表名 values();                //新增一整行資料

                      insert into 表名(sno) values(xx);    //對對應的列新增資料


        2、改: update 表名 set sno=xx;                預設修改所有的資料
                      update 表名 set sno=xx where uname=xxx;   只會修改uname為xxx的行


        3、刪: delete from 表名;                                        刪除所有的資料
                      delete from 表名 where uname=xxx;         只會刪除uname為xxx的行
        
        4、查:


            select * from 表名;              查一個表裡面所有的資料
            select uname from 表名;     查一個表裡面所有列為uname的資料
            select * from 表名 where age>18;                         查詢出所有年齡大於18歲的資料
            select * from 表名 where age>18 and sex='男';    查詢出所有年齡大於18歲的男性的資料
            select * from 表名 where age>18 or sex='男';       查詢出所有年齡大於18歲的,或者男性的資料
            select * from 表名 group by xxx;        以xx為分組查詢,只會查出這一列的所有的不同的值,並且,只有一行資料
            select * from 表名 order by xxx asc;  排序, 根據xxx來排序    asc從低到高      desc從高到低
            select * from 表名 limit x,y;                 limit限制查詢結果的數量
                如果只有一個引數,那麼代表從1開始,返回x條資料
                如果有兩個引數,代表從x開始,查詢y條資料
                
                當sql語句分組或者排序之後,就不能有條件了,如果想加條件,則用having
                要求年齡小於35歲才進行分組
                select * from 表名 group by sex where age<35;
                select * from 表名 group by sex having age<35;
                
           案例:
查出消費最高的那個人
                      select * from 表名 order by price desc limit 1;