以VMware Workstation Pro 15环境下的NeoKylin7.0与DM8为例
一、 基础CRUD操作
二、数据查询
三、多表联查
四、子查询
1.插入数据
insert into SLL.I_USER values (15812345678,124346);
insert into SLL.I_USER values
(15823456789,124356),
(15812345679,123435);
create table SLL.i_stul as select stuNo,stuName from SLL.I_STU;
2.删除数据
delete from 模式名.表名[where 条件];
delete from SLL.i_user where phone = 15123456789;
3.修改数据
update模式名.表名 set 列1=值1,……列n=值n [where 条件];
4.查询数据
select 列名1,……,列名n from 模式名.表名 [where 条件];
--查询i_user表的所有信息
select *from SLL.i_user;
--查询i_user表内的phone值为‘15123456789’的信息
select *from SLL.i_user where phone = 15123456789;
1.关系运算符:>、<、=、!=、>=、<
select * from i_goods where goods_id <> 1005;
2.逻辑运算符:and(与)、not(非)、or(或)
select * from i_goods where goods_id<1010 and price>25;
select * from i_goods where goods_id<1010 or discount=0.8;
select * from i_goods where not goods_id<1005;
3.范围运算
select * from i_goods where goods_id between 1005 and 1008;
select * from i_goods where goods_id in(1008,1030,1035);
4.模糊查询l–ike
--含桃的信息
select * from i_goods where goods_title like '%桃%';
--以香飘飘开头的信息
select * from i_basic where nick_name like '香飘飘%';
5.查询排序
select * from i_goods order by price desc;
6.聚合函数
select count(goods_id) as sum_number from i_goods;
select avg(price) as "平均价格" from i_goods;
select max(price) as "最高价格" from i_goods;
select min(price) as "最低价格" from i_goods;
select sum(price) as "总价格" from i_goods;
1.笛卡尔集查询
select 列名列表 from 表1,表2;
2.内联接查询
-- 内连接查询的方法一
-- 查商品信息: 商品编号,商品标题,商品价格,商品类型的名称
select g.GOODS_ID,g.GOODS_TITLE,g.PRICE,c.CATEGORY_NAME
from i_goods g,i_category c
where g.CATEGORY_ID=c.CATEGORY_ID
order by g.GOODS_ID;
-- 内连接查询的方法二
--查商品信息: 商品编号,商品标题,商品价格,商品类型的名称
select g.GOODS_ID,g.GOODS_TITLE,g.PRICE,c.CATEGORY_NAME
from i_goods g inner join i_category c
on g.CATEGORY_ID=c.CATEGORY_ID
order by g.GOODS_ID;
3.外联接查询
select g.GOODS_ID,g.GOODS_TITLE,g.PRICE,c.CATEGORY_NAME
from i_goods g left join i_category c
on g.CATEGORY_ID=c.CATEGORY_ID
order by g.GOODS_ID;
select g.GOODS_ID,g.GOODS_TITLE,g.PRICE,c.CATEGORY_NAME
from i_goods g right join i_category c
on g.CATEGORY_ID=c.CATEGORY_ID
order by g.GOODS_ID;
select g.GOODS_ID,g.GOODS_TITLE,g.PRICE,c.CATEGORY_NAME
from i_goods g full join i_category c
on g.CATEGORY_ID=c.CATEGORY_ID
order by g.GOODS_ID;
1.where子句子查询:将一个查询的结果作为另一个查询操作的条件
2.from子句子查询:将一个查询的结果看作一张虚拟表提供给其他查询使用
--查询统计订单个数>10的用户的总人数
select count(num) "符合条件的总人数" from (
select user_id num from i_order
group by user_id
having count(order_id)>10
)t;
3.select子句子查询:将一个查询的结果喝其他查询的结果进行组合
-- 查询所有商品编号,商品标题,商品的一级类型名称
select goods_id,goods_title,(
select category_name from i_category where category_id=(
select parent_id from i_category c where C.CATEGORY_ID=i_goods.category_id
)
) 一级类型名称 from i_goods;