亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 45
粉絲 0
評論 0
訪問量 45976
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
mysql數(shù)據(jù)庫CURD中常用操作
咸魚老爺
原創(chuàng)
775人瀏覽過
  • 更新 update
    增加一個(gè)年齡字段
    alter table user add age int unsigned not null default 0 comment '年齡' after gender;
    更新年齡數(shù)據(jù)
    update user set age= timestampdiff(year,borthday,now());

    條件更新
    update user set name='xiaowang' where name='wang';
  • 刪除
    delete from user where id=1;
    修改起始主鍵
    alter table user auto_increment =1;
    清空表
    truncate 表名;
  • 查詢
    SELECT [DISTRINCT] 表達(dá)式1|字段…(*表示所有列)
    FROM 表名 別名 [,數(shù)據(jù)源2 別名2]
    WHERE 查詢條件
    GROUP BY 分組字段1…
    HAVING 分組篩選條件1,…
    ORDER BY 排序字段1 ASC|DESC, 排序字段2 ASC|DESC
    LIMIT 偏移量 數(shù)量;

查詢數(shù)據(jù)庫
select database()
查詢版本
select vsersion()
查詢當(dāng)前時(shí)間
select now();
select * from user where id>950;

select name as 姓名,count(1) as數(shù)量 from user group by name;
select name 姓名,count(1) 數(shù)量 from user group by name;

  • 排序查詢
    select * from user where name='li' order by id desc;
  • 分頁查詢
    limit 顯示數(shù)量 offset 偏移量(跳過的記錄數(shù)量)
    limit 偏移量,顯示數(shù)量
    offset=(page-1)num;在查詢語句的最后寫
    前倆條 等價(jià)于limit 0,2;
    ` select
    from user order by age desc limit 2;![](https://img.php.cn/upload/image/669/941/590/1616663725280251.png) 從2到3select from user order by age desc limit 2 offset 1;![](https://img.php.cn/upload/image/163/911/787/1616663736497138.png) 年齡最大select from user where age=(select max(age) from user);`
  • 區(qū)間查詢
    1. select * from user where id>=10 and id <=20;
    2. select * from user where id between 10 and 20;
  • 集合查詢
    select * from user where id in (1,3,5);
  • like
    查詢name以z開始的
    select * from user where name like 'z%';

    查詢name包含a的
    select * from user where name like '%a%';

    查詢name以g結(jié)尾的
    select * from user where name like '%g';
  • 聚合函數(shù)
    max()最大值,min()最小值,sum(最大值),avg()平均數(shù),count()數(shù)量,四舍五入round(x,d) ,x指要處理的數(shù),d是指保留幾位小數(shù)
  • 分組查詢
    查詢男女人數(shù)
    select gender ,count(*) from user group by gender;

    查詢男女平均年齡
    select gender ,avg(age) 平均年齡 from user group by gender;

    分組帶條件查詢,having,不能用where查詢
    select gender ,count(*) from user group by gender having gender='male';
  • 關(guān)聯(lián)查詢
    • 內(nèi)鏈接
      select a.id,a.title,b.name from articles a, cates b where a.cid=b.cid;

      使用inner jion on簡化
      1. select id,title,name from articles a inner join cates b on a.cid=b.cid;
      2. //簡化
      3. select id,title,name from articles a join cates b using(cid);
      4. //過濾
      5. select id,title,name from articles a inner join cates b on a.cid=b.cid where a.cid=1;
    • 外連接
      左外連接,左表文章表為主表,查詢所有主表的信息,關(guān)聯(lián)表只列出匹配的數(shù)據(jù),沒有匹配到的為null
      select * from articles a left join cates b on a.cid=b.cid;

      右外連接
      select * from articles a right join cates b on a.cid=b.cid;

      外連接轉(zhuǎn)內(nèi)連接,將從表為null的字段過濾掉
      select * from articles a left join cates b on a.cid=b.cid where a.cid is not null;
    • 自然連接
      自然連接是內(nèi)連接的一個(gè)特例,前提是關(guān)聯(lián)表中存在同名字段。natural join
      select id,title,name from articles natural join cates;

      視圖

      創(chuàng)建視圖
      create view v_user as select * from user; create view vc_user as select name,age from user;
      查詢視圖
      select * from vc_user;

      更新視圖,會同步更新基本表
      update vc_user set age =40 where name='wang';

      刪除視圖
      drop view vc_user;

      索引

      索引應(yīng)該創(chuàng)建在經(jīng)常被查詢的字段,或者常出現(xiàn)在結(jié)果集的字段上
      索引分類:普通索引,主鍵索引,唯一索引,全文索引。
      create index 索引名稱 on 表名(表字段);
      創(chuàng)建索引
      1. //普通索引
      2. create index age on user(age);
      3. //唯一索引
      4. create unique index unique_age on user(age);
      5. //主鍵索引
      6. alter table user add primary key id(id);
      查詢索引
      show index from user;
      刪除索引
      drop index age on user;

      預(yù)處理

      防止sql注入攻擊,sql語句中的數(shù)據(jù)只有在執(zhí)行階段才會和字段進(jìn)行綁定。也可以看做是某一條sql語句的模板,可以多次重復(fù)調(diào)用提高效率
      prepare生成預(yù)處理sql語句
      1. prepare user from 'select * from user where age > ? limit ? ';
      2. 將真實(shí)的數(shù)據(jù)綁定到預(yù)處理語句中的占位符上
      3. set @age =10;
      4. set @num=5;
      5. execute user using @age,@num;
批改老師:天蓬老師天蓬老師

批改狀態(tài):合格

老師批語:
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報(bào)處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
作者最新博文
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)