<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 查詢數據 ``` select 列名1, 列名2, from 表名 where條件 ``` ## 語法: ``` select*from user where sex!=1; //sex為表的列字段 select*from user where sex=1; //sex為表的列字段 select*from user where age=40 or age=23; //age為列字段 這個意思是40或者23 select*from user where user_name='liudehua'and age=36; //兩個條件 select*from user where age between 30 and 60; //between用法 select*from user where age in(36,33); //in() 用法 select*from user where age not in(36,33); // no in() 用法 select*from user where user_name like '%u%'; //模糊查詢用法 這里是查含字符串里有u字符的。 select*from user where user_name like '%g'; //模糊查詢以某個字符開頭實例, 這里是查含字符串里以g開頭的字符串。 select*from user where user_name like 'g%'; //模糊查詢以某個字符結尾實例, 這里是查含字符串里以g結尾的字符串。 查所有字段用* 不帶where條件 把表的所有記錄查出來 ``` # 條件付號 ``` and并且的意思與&&相同 or 與 || 相同 < > <= >= != = 邏輯運算符。 between 30 and 60 范圍 (多少到多少的意思,這里是30-60的意思) in(36,33); 與or大至相同 not in(36,33); 非36和33的 like '%這里寫含有某個字符字符%'; 模糊查詢(這個查的必須是字符串) like '這里寫含有某個字符字符%'; 以這個字符串開始 模糊查詢 (這個查的必須是字符串) like '%這里寫含有某個字符字符'; 以這個字符串結束 模糊查詢(這個查的必須是字符串) ``` # 改別名 意思:把查詢回來的名稱改名 ## 語法: ``` select 字段名稱 as 改后的字段名稱 from 數據庫表名; 代碼案例:select user_name as name from user; ``` # 字段值連接一起 ## 語法: ``` concat() select concat(字段名,字段名,字段名) from 數據庫表名; //可以把幾個字段連到一起 select concat_ws("分隔符==",字段名,字段名,字段名) from 數據庫表名 //可以用分割連接一起看起來更清楚 ``` ## 代碼案例: ``` select concat_ws('==',user_name,age,sex) from user; ``` # 過濾重復值 ## 語法: ``` select distinct 字段名 from 數據庫表名; 代碼案例:select distinct user_name from user; ``` # LIMIT 查詢 限制查詢的條數 ## 語法: ``` select *from 表名 limit (從第幾個開),(到后面幾個) //從1后面開始的2個 代碼案例: select*from user limit 0,3; //從第0個開始后面的3個表 ``` # 聚合函數 ## 查詢行總數 ###語法: ``` select count(*) from 表名 // 查詢表行數 一共多少行 代碼案例: select count(id)from user; //查表中id行數 ``` ## 求查詢字段的和值 ###語法: ``` select sum(字段名) from 表名; ``` ## 求查詢字段的平均值 ``` select avg (字段名) from 表名; 代碼案例: select avg(age)from user; //求age字段平均值 ``` ## 求查詢字段的最大值 ``` select max(字段名) from 表名 代碼案例: select max(age)from user; //求age的最大值 ``` ## 求查詢字段的最小值 ``` select min(字段名) from 表名 代碼案例: select min(age)from user; //求age的最小值 ``` # 分組查詢(聚合查詢) ## 語法: ``` select 要分組的字段 from 表名 group by 要分組的字段; //把字段里共同的值分到一個組里 ``` ## 代碼案例: ``` select sex from user group by sex; //sex字段分組分組 select avg(age), sex from user group by sex; //兩條語句執行 先求平均age(年齡),在把sex(男女)進行分組 ``` # 分組查詢條件查詢 ## 語法: ``` select 要分組的字段 from user group by 要分組的字段 having 條件 ; ``` ## 代碼案例: ``` select avg(age), sex from user group by sex having count(age)>2; //兩條語句執行 先求平均age(年齡),在把sex(男女) 并且年齡總行數大于2的 ``` # 連表查詢 ``` 2個表或者多個表查詢 內連接查詢 inner join(顯示和隱式) ``` 笛卡爾積 ## 連表查詢隱式方法: ## 語法: ``` select*from 表名,另一個表名稱 where 條件 select 表名.字段名 from 表名,另一個表名稱 where 條件 //這里的點語法與JS一樣 (點語法這里也可也用別名法) ``` ## 連表查詢內連接(顯示方法) ``` select*from 表名inner join 另一個表名稱 on 條件 //顯示方法(條件也是用點語法) ``` 代碼案例: ~~~ select*from guo_article inner join guo_cat on guo_article.cat_id=guo_cat.id; ~~~ ![](https://img.kancloud.cn/a1/41/a1410c20670d87b7f213d5e26aa5751a_1318x81.png) ``` select*from student,mark where mark.stu_id=student.id; //這個意思就是student與mark表一起查 條件里的stu_id是mark的 ,id是student的 這里用的是點語法,與JS的點語法一樣,就是包含的意思(父包含子,父里的什么什么子) select*from student,mark where mark.stu_id=student.id and student.id=1; //student與 mark表一起查出,并且查出student.id=1的數據 ``` 代碼案例 : ``` select s.name,s.mobile, m.mark,s. id from student as s left join mark as m on m.stu_id=s.id; ``` ## 連表查詢左連接 ``` 兩個表以左邊數據數量為主,右邊不夠給自動補償 select*from 表名 left join 另一個表名稱 on 條件 代碼案例: select*from student left join mark on student.id=mark.stu_id; ``` ## 連表查詢右連接 ``` 兩個表以右邊數據數量為主,左邊多出去的不展示 select*from 表名 right join 另一個表名稱 on 條件 代碼案例: select*from student right join mark on student.id=mark.stu_id; ``` ## 連表查詢內連接 ``` 只要有數據的時候就顯示出來,沒有的不顯示(比較智能,用的多,推薦用) select*from 表名inner join 另一個表名稱 on 條件 注意:inner left right 不能用where條件否則報錯。 ``` # 子查詢 select*from 表名 where 字段名 in(外面查詢語句的一個條件 ) ## 代碼案例: ``` select*from student where id in(select id from student where id>1); //括號里其實是一個條件語句,也就是子查詢。 ``` # 聯合查詢 select 字段名 from 表名 union select 字段名 from 第二個表名 ## 代碼案例: ``` select id,neme from php union select id,neme from java; select 字段名 from 表名 union all select 字段名 from 第二個表名 select id,neme from php union all select id,neme from java; ``` ## union與union all 的區別 ``` 當用union 兩個表的字段值位置內容如果相同(比如id1都是同一個值),就會去掉一個, 用union all的時候兩個表的字段值位置內容如果相同(比如id1都是同一個值),會保留這個2個值,一起展示出出來。 注意:字段必須相等 也就是查詢的時候必須是同樣個數的字段,共同查1個或者2個。不能表1查一個字段,表2查二個字段。 ``` # union 與join區別 ## union只是將兩條select語句的查詢結果組合在一起,而join是將兩個表建立起關聯后再查詢。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看