创建表
字段(列)名: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;