MySQL基本操作

创建表
字段(列)名:id,类型:int,默认空间11,约束条件:不为空,自增
name,char,不为空,默认值:空,注释:站点名称
id为主键
UTF-8

CREATE TABLE `websites` (
id int(11) NOT NULL AUTO_INCREMENT,
name char(20) NOT NULL DEFAULT '' COMMENT '站点名称',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

删除表

DROP TABLE IF EXISTS `websites`;


插入记录(行)

INSERT INTO websites(name, url,alexa,sal,country ) VALUES ('腾讯', 'https://www.qq.com',18, 1000,'CN' ) ;


从websites表中删除id为5的记录

delete from websites where id = 5;


将id为3的记录中sal改为null

update websites set sal = null where id = 3;


显示表中所有记录(不限字段)

select * from websites;

显示表中记录的 唯一的 country字段

select distinct country from websites order by sal asc,alexa desc ;--先根据sal 升序排序,再根据 alexa 降序;

分页查询

select * from websites limit 2,3 ; --从第2条(下标从0开始)开始查,查3条数据

分组查询
avg(sal) 起别名为 aa,当sal存在时按country分组aa>1500的显示

select avg(sal) aa from websites where sal is not null group by country having aa > 1500;

连接查询
左外连接,websites别名w,access_log表别名a,显示w表与a表中,w中的id=a表中site_id对应的记录的name字段

select name from websites w left join access_log a on w.id = a.site_id;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值