首页 > 代码库 > mysql
mysql
建表语句:
create table 表名(列名称,列类型 [列属性][默认值]),
engine 引擎名 charset 字符集
增:
往那张表增,增哪几列,各为什么值;
insert into 表名 (列1,列2, ... 列n)
values
(值1, 值2.......值N)
如果不声明拆入的列,则默认拆入所有列;
改:update
修改哪张表,修改哪几列,修改成什么值?
在哪几行上生效
update 表名
set
列1 = 值1
列2 = 值2
.。。
列N = 值N
where 表达式
delete:
删除哪几张表的数据,删那些行
delete from 表名
where 表达式
查:
select * from 表名
查询5种子句:where 后面的表示式子代入到每行,确认是否成立;
where shop_price - market_price > 200;
in(值1, 值2, 值3, ...值N)等于值1-N任意之一,都可以;
select good_id, cat_id from goods where cat_id in (4, 5);
between 在某一范围内;
between 值1 and 值2, 表示在值1和值2之间(允许等于边界);
select good_it, cat_id from goods where cat_id between 1 and 6;
or用法
select good_id, good_name, shop_price from goods where shop_price >=3000
and shop_price <=5000 or shop_price >=500 and shop_price<=1000;
not的用法:
select good_id, cat_id from goods where cat_id not in (4, 5);
select good_id, cat_id from goods where cat_id!=4 and cat_id != 5;
模糊查询:‘ % ’:通配任意字符 ‘_‘:单个字符
select good_id, cat_id from goods where good_name like ‘%诺基亚%‘;
group by (要和聚合函数(统计函数)一起使用)
作用:把行 按 字段 分组
语法:group by col1, col2, ... colN
运用场合:
常见于统计场合,如按栏目计数帖子数,
统计每个人的平均成绩等;
max(shop_price) //聚合函数
select good_id, good_name, max(shop_price) from goods; //语法是错误的
select min(shop_price) from goods; //ok
select cat_id, max(shop_price) from goods group by cat_id;
select min(shop_price) from goods;
select min(goods_id) from goods;
select sum(goods_number) from goods;
select avg(shop_price) from goods;
计算表中函数
select count(*) from goods;
select cat_id, min(shop_price) from goods group by cat_id; //ok
select cat_id, count(*) from goods group by cat_id;
select good_id, good_name, market_price-shop_price from goods;
select cat_id, sum(shop_price *good_number) from goods group by cat_id;
给列取别名: as
select cat_id, sum(shop_price*goods_number) as huokuan from goods group by cat_id;
mysql