<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      mysql常用語句及用法

      [摘要]本篇文章主要介紹mysql常用語句,感興趣的朋友參考下,希望對大家有所幫助。1.mysql的基本操作 1.開啟mysql數據庫服務 格式: net start mysql ...
      本篇文章主要介紹mysql常用語句,感興趣的朋友參考下,希望對大家有所幫助。

      1.mysql的基本操作

      1.開啟mysql數據庫服務
      格式: net start mysql

      如: net start mysql57

      2.關閉mysql數據庫服務
      格式: net stop mysql
      如: net stop mysql57

      3.登陸mysql服務
      格式: mysql -u root -p
      如: mysql -u root -p 執行成功后,在輸入密碼,

      4. 查看當前數據庫的版本xinxi
      格式: select version();


      5.查看當前時間
      格式: select now();


      6.退出,

      quit,exit

      2.數據庫database的操作


      1.查看當前有哪些數據庫

      格式: show databases;

      2.創建一個數據庫

      格式: create database 數據庫名 charset=utf8;

      例: create database wen charset=utf8;

      3.刪除一個數據庫

      格式: drop database 數據庫名;

      例: drop database wen;

      4.使用數據庫

      格式:use 數據庫名;

      例: use wen;

      5.查看當前使用數據庫

      格式: select database();

      3.表table的操作

      1.查看當前數據庫有哪些表

      格式: show tables;

      2.創建一個表

      格式: create table 表名(字段名及類型);

      例: create table student(id int auto_increment primary key,name varchar(20) not null,age int not null,address varchar(20),gender bit default 1);

      3.查看表的結構

      格式: desc 表名;

      例: desc student;


      4.查看創建表的語句

      格式: show create table 表名;


      5.刪除表

      格式: drop table 表名;

      例: drop table student;


      6.修改表

      1.修改表的名字

      格式: rename table 舊表名 to 新表名;

      例: rename table student to students;


      2.修改表的結構, add drop change

      1.add 添加一個新的字段

      格式: alter table 表名 add 字段名 類型;

      例: alter table student add phone int;

      2.change 修改

      格式: alter table 表名 change 舊字段名 新字段名 新字段類型;

      例: alter table student change phone number varchar(20);


      3.drop 刪除

      格式: alter table 表名 drop 字段名;

      例: alter table student drop number;

      4.數據的操作

      1.插入數據

      1.插入全列數據

      格式: insert into 表名 values(值1,值2,....)

      注意:值得順序需要與表的結構對應, 主健可以寫0  

      例: insert into student values(0,"小明",36,"北京朝陽區",1);

        2.缺省插入

         格式:insert into 表名(字段名1,字段名2,....) values(值1,值2,....) 

           例: insert into student(name,age,address) values("小東",48,"深圳南山區");

      3.插入多條數據

      格式: insert into 表名 values(值1,值2,....),(值1,值2,....),(值1,值2,..)

      例: insert into student values(0,"小云",45,"杭州",0),(0,"小雨",105,"俄羅斯",0),(0,"小清",99,"日本",0);

      2.刪除數據

      格式: delete from 表名 where 條件

      例: delete from student where age = 105;

      delete from student

      不加條件表示全部刪除, 請謹慎使用

      3.修改數據

      格式:update 表名 set 字段名1=值,字段名2=值,... where 條件;

      update student set gender = 1 where name = "小青";

      update student set age = 52,address="深圳" where name = "小東";

      4.查看所有數據

      格式: select * from 表名;

      例: select * from student;

      5.查詢數據

      1.查詢格式

      1.查詢所有

      格式: select * from 表名;

      2.按條件查詢

      格式: select 字段名1,字段名2,... from 表名 where 條件;

      1.select后跟的是查詢結果需要顯示的字段名

      2. * 表示所有的字段名都要顯示

      3. from 后跟的是表名,表示從那個表查

      4. where 后跟的是篩選條件

      5. 可以使用 字段名 as 別名 來給字段名取個別名

      例: 顯示 name和age

      select name,age from student;

      select name as "姓名" from student;

      2.條件中的運算

      1.比較運算符

      > 大于

      < 小于

      >= 大于等于

      <= 小于等于

      = 等于

      != 不等于

      格式: select 字段名1,字段名2,... from 表名 where 字段名 比較運算符 值;

      例: select * from student where age < 50;

      2. 邏輯運算符

      and 且

      or 或者

      not 非

      格式: select 字段名1,字段名2,... from 表名 where 字段名 比較運算符 值 邏輯運算符 字段名 比較運算符 值 ...;

      例: select * from student where age < 50 and age > 40;

      select * from student where not (age < 50 and age > 40);

      3.模糊運算符 like

      % 表示多個任意字符

      _ 表示一個任意字符

      需求: 匹配所有姓小的人
      格式: select * from student where name like "小%";
      格式: select * from student where name like "小_";
      需求: 匹配名字中有 "小"字的
      select * from student where name like "%小%";


      4.范圍查詢
      in (多個值 ) 判斷一個值 是否是多個值中的一個
      between 值1(包含) and 值2(包含) 判斷一個值是否在 值1與值2之間
      格式: select 字段名1,字段名2,... from 表名 where 字段名 范圍運算符 值;
      例: 找出 25或者45或者80或者 90
      select * from student where age in (25,45,80,90);
      需求:找出25 到 45之間的數據
      select * from student where age between 25 and 45;

      5. 空判斷
      is null 為空
      is not null 不為空
      格式: select 字段名1,字段名2,... from 表名 where 字段名 is null;
      插入數據: insert into student(name,age) values("小·超",60);
      例: select * from student where address is null;
      select * from student where address is not null;

      6.去除重復的值
      格式: select distinct 字段名 from 表名 where 條件;
      例: select distinct gender from student;

      7.聚合函數
      count(*) 求當前結果總共有多少條數據
      sum(列名) 求列名對應列的 和
      avg(列名) 求當前列的平均值
      max(列名) 求當前列的最大值
      min(列名) 求當前列的最小值
      例: 求當前表總共有多少條數據?
      select count(*) from student;
      求年齡最小的?
      select min(age) from student;

      8.分組 group by
      格式: select 字段名... from 表名 where 條件 group by 字段名

      查看有多少種性別

      例: select gender from student group by gender;

      需求:統計 男生 和 女生 各有多少個
      select gender,count(*) from student group by gender;
      需求: 統計所有女生的個數?
      例: select gender,count(*) from student group by gender having gender = 1;
      where 查詢條件, 是先執行的查詢條件
      having 查詢條件 是在查詢結果的基礎上在查詢

      9. 排序
      格式: select 字段名... from 表名 where 條件 order by 字段名1,字段名2...
      例: 年齡小到大
      select * from student order by age;
      默認是從小到大排列
      asc 從小到大
      desc 從大到小
      select * from student order by age asc;

      10. 分頁

      格式: select 字段名... from 表名 where 條件 limit 起始值,多少條數據
      起始值可以從 0 開始
      例: select * from student limit 0,3;

      相關推薦:

      MySQL常用語句簡介

      MySQL常用語句講解匯總

      Php按時間查詢 Mysql常用語句

      以上就是mysql常用語句及用法的詳細內容,更多請關注php中文網其它相關文章!


      學習教程快速掌握從入門到精通的SQL知識。




      日韩精品一区二区三区高清