1.SQL语句及其种类
2.为列设定别名
select product_id AS id,product_name AS name from product;
select product_id AS "商品编号" , product_name AS “商品名称” from product;
3.删除重复数据
select distince product_type from product;
4.算术运算符和比较运算符
算术运算符 +,-,*,/
select sale_price , sale_price * 2 AS "sale_price * 2" from product;
比较运算符 >,<,<=,<>(不等于)
5.聚合函数
select count(*) AS count from product;
计算去除重复数据后的数据行数
select count(distinct product_type) from product;
6.分组 group by
select -> from -> where -> group by -> having
select product_price , count(*) from product
where product_type = "衣服"
group by product_price;
select product_price , count(*) from product
group by product_price;
having count(*) = 2;
7.视图
无需保存数据,节省存储设备的容量
创建视图
create viem productSum (product_type,cnt_product)
AS
select product_type,count(*) from product
group by product_type;
使用视图
select product_type,cntduct from productSum;
删除视图
drop viem productSum;
8.函数的种类
ABS(绝对值),MOD(求余),round(四舍五入)
select m , ABS(m) AS abs from product;
select n,p , MOD(n,p) AS mod from product;
round(对象数值,保留小数的位数)
||(拼接),length(字符串长度),lower(小写转换),upper(大写转换),replace(字符串的替换),substring(字符串的截取)
select str1,str2,str1 || str2 AS str_concat from product;
select str, length(str) from product;
replace(对象字符串,替换前的字符串,替换后的字符串)
subString(对象字符串 from 截取的初始位置 for 截取的字符数)
9.like查询
前后中查询一致
select * from product where name like 'ddd%';
select * from product where name like 'ddd%';
select * from product where name like '%ddd%';
前方一致,后匹配3个字符
select * from product where name like 'ddd_ _ _';
10.谓词
between(范围查询),or,
where sale_price between 100 and 1000;
11.多表联结
内联结 inner join
外联结 outer join
select sp.id,sp.name,p.id,p.name
from product AS p inner join shopproduct AS sp
on sp.id = p.id
where sp.id="1";
12.事务
可进行多条sql语句执行
begin transaction;
sql语句;
commit; //提交事务
rollback; //回滚事务