1. mysql 十一章使用数据处理函数
11.1 函数:
例子:RTrim(),Trim(), LTrim(),
11.2使用函数
处理字符串的文本函数(supper(),lower())。
数值函数
日期和时间函数
系统函数
11.2.1 文本处理函数
代码如下:select vend_name, upper(vend_name) as vend_name_upcase from django_apps_vendor order by vend_name;
upper()将文本转换为大写
常用的文本处理函数:RTrim(),Trim(), LTrim(),Upper(),Lower(),SubString(), Length(), Left(), Right(), Locate().
Soundex()函数:
select cust_name, cust_contact from django_apps_customer where cust_contact = "Y.Lie";
把cust_contact= "Y.Lee"输入为cust_contact = "Y.Lie"结果没有数据返回
select cust_name, cust_contact from django_apps_customer where Soundex(cust_contact) = Soundex("Y.Lie");
使用这个函数进行搜索,它匹配所用发音类似于Y.Lie的联系名。
11.2.2 日期和时间处理函数https://wiki.jikexueyuan.com/project/mysql/useful-functions/time-functions.html
日期必须为格式yyyy-mm-dd
6月6号学习记录
1.MySQL-执行算术运算
select prod_id, quantity, item_price from orderitems where order_num = 20005;
select prod_id, quantity, item_price, quantity*item_price as expanded_price from orderitems where order_num = 20005;
quantity*item_price总的价格。
算术运算符的优先级,括号,加,减,乘,除。
如何去测试计算:
例子:select (3*2) ;
select RTrim('abc');
select now();
函数:upper()转换为大写
select vend_name, upper(vend_name) as vend_name_upcase from vendors order by vend_name;
soundex()根据发音比较而不是字母比较。
select cust_name, cust_contact from customers where cust_contact = "Y.Lie";
select cust_name, cust_contact from customers where soundex(cust_contact) = soundex("Y.Lie");
日期的简单查询:yyyy-mm-dd基本格式
select order_num, cust_id from orders where order_date = '2005-09-01';
select order_num, cust_id from orders where date(order_date) = '2005-09-01';
select cust_id, order_num from orders where date(order_date) between '2005-09-01' and '2005-09-30';日期范围
select cust_id, order_num from orders where year(order_date) = 2005 and month(order_date) = 9;
这个不需要记住每个月有多少天,或者不需要关心闰年2月的方法。
数值函数;:
select mod(6,3) select sqrt(8); select exp(8); select abs(-1);select cos(1);select rand();select pi()
2.mysql-汇总数据
聚集函数:运行在行组上,计算和返回单个值的函数。
确定表中的行数, 获得表中行组的数据, 找出表列的最大值,最小值和平均值。
mysql给出5个聚集函数。
AVG() 平均值 count()行数 max()最大值 min()最小值 sum()值之和
实例:select avg(prod_price) as avg_price from products;
count():
select count(*) as num_cust from customers;所用的行,忽略null
select count(cust_email) as num_cust from customers; 特定的行。不忽略null
max():
select max(prod_price) as max_price from products;
mix():
select min(prod_price) as min_price from products;
sum(): 返回指定列值的和。
select sum(quantity) as items_ordered from orderitems where order_num = 20005;求20005这个订单号,数量的和。
select sum(quantity*item_price) as items_ordered from orderitems where order_num = 20005;
求20005物品价钱的和,
select avg(distinct prod_price) as avg_price from products where vend_id = 1003;
组合聚集函数使用:select count(*) as num_items, min(prod_price) as price_min, max(prod_price) as price_max, avg(prod_price) as price_avg from products;
6月7号学习记录
一. MySQL
1.分组数据
select count(*) as num_prods from products where vend_id = 1003;
返回供应商1003提供的产品数目。
2.创建分组:group by 子句
select vend_id, count(*) as num_prods from products group by vend_id;
group by按vend_id 排序并分组数据。
group by 子句的一些规定:
<1> group by子句必须出现在where子句之后,order by子句之前。
<2> group by子句可以包含任意数目的列。
<3>在建立分组时,指定的所有列都一起计算。如果在group by子句中嵌套了分组,数据将在最后的分组上进行汇总。
<4> group by子句中列出的每个列必须是检索列或有效的表达式(但不能是聚集函数)。
如果在select中使用表达式,则必须在group by子句中指定相同的表达式。不能使用别名。
<5>除聚集函数语句外,select语句中的每个列都必须在group by子句中给出。
<6>如果分组列中具有null值,则null将作为一个分组返回。有多行null值,他们将分为一组。
3. with rollup 和 coalesce
select count(*) as num_prods from products group by vend_id with rollup;
把总的行数在进行汇总, 返回null
把返回null的设置一个取代名称,语法:coalesce(a,b,c),
如果a==null则选择b,b==null则选择c
select coalesce(vend_id, '总和:'), count(*) as num_prods from products group by vend_id with rollup;
4. 过滤分组having
select cust_id, count(*) as orders from orders group by cust_id having count(*) >= 2;
先用group by子句进行分组,然后再用having子句进行过滤分组。
select vend_id, count(*) as num_prods from products where prod_price >= 10 group by vend_id; 先进行分组,找出所有符合条件的
select vend_id, count(*) as num_prods from products where prod_price >= 10 group by vend_id having count(*) >= 2;在过滤
5.分组和排序group by 和order by 结合使用
select order_num, sum(quantity*item_price) as ordertotal from orderitems group by order_num having sum(quantity*item_price) >= 50 order by ordertotal;
第一步先进行分组:
select order_num, sum(quantity*item_price) as ordertotal from orderitems group by order_num;
第二步进行分组过滤
select order_num, sum(quantity*item_price) as ordertotal from orderitems group by order_num having sum(quantity*item_price) >= 50;
最后一步:进行排序
select order_num, sum(quantity*item_price) as ordertotal from orderitems group by order_num having sum(quantity*item_price) >= 50 order by ordertotal;
6.select字句的循序
select from where gruop by having order by limit
select 要返回列或表达式,
from 从中检索数据的表, 仅在从表选择数据时使用
where 行级过滤
group by 分组说明, 仅在按组计算聚集时使用
having 组级过滤
order by 输出排序顺序
limit 要检索的行数
Django3.xx——xadmin 报错处理总结
错误1.AttributeError: module ‘django.db.models‘ has no attribute ‘FieldDoesNotExist‘
解决方法:
from django.core import exceptions
问题2:IndexError: list index out of range
解决方法:
我的解决方法:(用><分组)
将:
input_html = [ht for ht in super(AdminSplitDateTime, self).render(name, value, attrs).replace('/>n 改为:
input_html = [ht for ht in super(AdminSplitDateTime, self).render(name, value, attrs).split('><') if ht != '']
input_html[0] = input_html[0] + ">"
input_html[1] = "<" + input_html[1]
6月8号学习记录
1.MySQL——子查询
select 语句是SQL的查询。select语句的简单查询,从单个数据库表中检索数据的单条语句。
查询(query):任何SQL语句都是查询。但此术语一般指select语句。
SQL语句还允许创建子查询,既嵌套在其他查询中的查询。
<1> 利用子查询进行过滤
例子:假如需要列出订购物品TNT2的所有客户,应该怎么检索?
步骤:第一步:检索包含物品TNT2的所有订单的编号。
select order_num from orderitems where prod_id = "TNT2"; 返回结果为(20005, 20007)
第二步:检所具有前一步骤列出的订单编号的所有客户的ID。
select cust_id from orders where order_num in (select order_num from orderitems where prod_id = "TNT2"); 或者select cust_id from orders where order_num in (20005, 20007);返回结果为(10001, 10004)
第三部:检索前一步骤返回的所有客户ID的客户信息。
select cust_name, cust_contact from customers where cust_id in (select cust_id from orders where order_num in (select order_num from orderitems where prod_id = "TNT2"));
或者select cust_name, cust_contact from customers where cust_id in (10001, 10004);
上述的每一个步骤都可以单独作为一个查询来执行。
可以把一条select语句返回的结果用于另一条select语句的where子句。
在select语句中,子查询总是从内向外处理。
注意:in操作符作用是传递给外部查询的where子句。子查询一般与in操作符结合使用。但也可以用于测试,等于(=),不等于(!=,<>)等。
<2> 作为计算字段使用子查询,另一种子查询使用方式
select cust_name, cust_state, (select count(*) from orders where orders.cust_id = customers.cust_id) as orders from customers order by cust_name;
这里涉及到相关子查询:设计外部查询的子查询。任何时候只要列名可能有多义性,就必须使用这种语法,表名和列名由一个句点分割。
where orders.cust_id = customers.cust_id



