提示:以下是本篇文章正文内容,下面案例可供参考
有一个商店的数据,记录客户及购物情况,有以下三个表组成:
商品goods(商品编号goods_id,商品名goods_name, 单价unitprice, 商品类别category, 供应商provider)
客户customer(客户号customer_id,姓名name,住址address,邮箱email,性别sex,身份证(card_id)
购买purchase(购买订单号order_id,客户号customer_id,商品号goods_id,购买数量nums)
default character set utf8 ;
不要忘记这个语句,我这个低版本的mysql;
create table if not exists goods(goods_id int,
-> goods_name varchar(32),
-> unitprice int,
-> category varchar(12),
-> provider varchar(32));
mysql> create table if not exists customer(
-> customer_id int comment '客户编号',
-> name varchar(32) comment '客户姓名',
-> address varchar(256) comment '客户地址',
-> email varchar(64) comment '电子邮箱',
-> sex bit comment '性别',
-> card_id varchar(18) comment '身份证');
mysql> create table if not exists purchase
-> (order_id int comment '订单号',
-> customer_id int comment '客户编号',
-> goods_id int comment '商品编号',
-> nums int comment '购买数量');
mysql> INSERT INTO goods VALUES
-> (1, 'Product1', 100, 'Category1', 'Provider1');
INSERT INTO goods (goods_id, goods_name, unitprice, category, provider)
VALUES
(1, 'Product1', 100, 'Category1', 'Provider1'),
-- 插入其他14行数据
(15, 'Product15', 150, 'Category3', 'Provider3');
INSERT INTO goods (goods_id, goods_name, unitprice)
VALUES
(1, '商品1', 100),
(2, '商品2', 150),
-- 插入其他商品数据...
(15, '商品15', 120);
mysql> select goods_id+unitprice 总和 from goods;
语法:
SELECT DISTINCT math FROM goods;
asc升序,desc降序
SELECT name, chinese + english + math FROM exam_result
ORDER BY chinese + english + math DESC;
SELECT name, chinese + english + math total FROM exam_result
ORDER BY total DESC;
-- 查询同学各门成绩,依次按 数学降序,英语升序,语文升序的方式显示
SELECT name, math, english, chinese FROM exam_result
ORDER BY math DESC, english, chinese;
– 查询同学各门成绩,依次按 数学降序,英语升序,语文升序的方式显示
如果想让他们都降序
SELECT name, math, english, chinese FROM exam_result
ORDER BY math DESC, english DESC, chinese DESC;
默认是asc升序
DESC 缩写自 “descending”,表示降序排列。
ASC 缩写自 “ascending”,表示升序排列。
-- 查询英语不及格的同学及英语成绩 ( < 60 )
SELECT name, english FROM exam_result WHERE english < 60;
-- 查询语文成绩好于英语成绩的同学
SELECT name, chinese, english FROM exam_result WHERE chinese > english;
-- 查询总分在 200 分以下的同学
SELECT name, chinese + math + english 总分 FROM exam_result
WHERE chinese + math + english < 200;
-- 查询语文成绩大于80分,且英语成绩大于80分的同学
SELECT * FROM exam_result WHERE chinese > 80 and english > 80;
-- 查询语文成绩大于80分,或英语成绩大于80分的同学
SELECT * FROM exam_result WHERE chinese > 80 or english > 80;
-- 观察AND 和 OR 的优先级:
SELECT * FROM exam_result WHERE chinese > 80 or math>70 and english > 70;
SELECT * FROM exam_result WHERE (chinese > 80 or math>70) and english > 70;
--查询语文成绩在 [80, 90] 分的同学及语文成绩
SELECT name, chinese FROM exam_result WHERE chinese BETWEEN 80 AND 90;
-- 使用 AND 也可以实现
SELECT name, chinese FROM exam_result WHERE chinese >= 80 AND chinese
<= 90;
在mysql中用in
-- 查询数学成绩是 58 或者 59 或者 98 或者 99 分的同学及数学成绩
SELECT name, math FROM exam_result WHERE math IN (58, 59, 98, 99);
-- 使用 OR 也可以实现
SELECT name, math FROM exam_result WHERE math = 58 OR math = 59 OR math
= 98 OR math = 99;
模糊查询(like)
-- % 匹配任意多个(包括 0 个)字符
SELECT name FROM exam_result WHERE name LIKE '孙%';-- 匹配到孙悟空、孙权
-- _ 匹配严格的一个任意字符
SELECT name FROM exam_result WHERE name LIKE '孙_';-- 匹配到孙权
-- 将孙悟空同学的数学成绩变更为 80 分
UPDATE exam_result SET math = 80 WHERE name = '孙悟空';
-- 将曹孟德同学的数学成绩变更为 60 分,语文成绩变更为 70 分
UPDATE exam_result SET math = 60, chinese = 70 WHERE name = '曹孟德';
-- 将总成绩倒数前三的 3 位同学的数学成绩加上 30 分
UPDATE exam_result SET math = math + 30 ORDER BY chinese + math + english LIMIT
3;
-- 将所有同学的语文成绩更新为原来的 2 倍
UPDATE exam_result SET chinese = chinese * 2;
DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]
mysql> alter table student drop column sex;
基本语法就是选中某个然后删除某一列
下面这个是插入
mysql> alter table student add column sex varchar(20);
好了,这一篇mysqlblog到这里了