MySQL的命令语句

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 19:37   1801   0

sql语句分为DDL数据库定义语言,DML数据库操作语言,DCL数据库控制语言。

show databases; 显示数据库

create database 名字; 建库

use 库名;指定要使用的数据库

show table ; 显示数据库中的表

create table 表名; 建表

eg:create table user (id int ,name varchar(40),age int);

更新表的结构

为表中添加:alter table 表名 add colum 名字 类型;

删除:alter table 表名 drop colum 名字;

删除整个表: drop table 表名;

删除整个库:drop database 库名;

查询表的结构:desc 表名;

添加数据:insert into 表名 (列1,列2,····) values(1,2,·····);

如果添加的是全部的数据,则前面的列1,列2,可以省略。

直接insert into 表名 values(`````);

更新数据:

update 表名 set 列名=值; 全部修改

update 表名 set 列名=值 where name=“”;

update 表名 set 列名1=值1,列名2=值2,······,where ······;

删除表:drop table 表名;

清空表:delete from table 表名;

delete from 表名 where ·······;

查询表:

select * from 表名;查询表中的所有的数据。

起别名select goods_name as "商品名称" from goods; 则显示的表以别名出现;

如何在查询的表中删除重复行:select distinct 列名 from 表名 ;

select 列名from表名 where 列名·········;

模糊查询:select 列名 from 表名 where 列名 like “···”

如要查询手机,则 like "%机"

还有 in :

select goods_name from goods where id in(3,5,9);

聚合查询:

select count(列名)from 表名 计算表中具体某个列名的记入数;

select count(distinct goods_type)from goods; 可以记录不重复的种类;

分组

select goods_type,count(*) from goods group by goods_type;

过滤:则在最后加上 having count(*)=2;

排序:

select * from goods order by goods_price desc; 降序 ;升序为asc;

多表查询:

select * from user u inner join user_info i on u.info_id=i.info_id;

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP