个人博客学习记录

  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值,他们将分为一组。

  1. with rollup 和 coalesce

select count(*) as num_prods from products group by vend_id with rollup;

把总的行数在进行汇总, 返回null

把返回null的设置一个取代名称,语法:coalesce(a,b,c),

如果anull则选择b,bnull则选择c

select coalesce(vend_id, ‘总和:’), count(*) as num_prods from products group by vend_id with rollup;

  1. 过滤分组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(quantityitem_price) as ordertotal from orderitems group by order_num having sum(quantityitem_price) >= 50 order by ordertotal;
第一步先进行分组:

select order_num, sum(quantity*item_price) as ordertotal from orderitems group by order_num;

第二步进行分组过滤

select order_num, sum(quantityitem_price) as ordertotal from orderitems group by order_num having sum(quantityitem_price) >= 50;

最后一步:进行排序

select order_num, sum(quantityitem_price) as ordertotal from orderitems group by order_num having sum(quantityitem_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(‘/><input’, ‘/>\n<input’).split(‘\n’) if ht != ‘’]
改为:
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

2.python测试

11.1 测试函数

11.1.1 单元测试和测试用例

Python标准库中的模块unittest提供了代码测试工具

单元测试用于核实函数的某个方面没有问题;
测试用例是一组单元测试,这些单元测试一起核实函数在各种情形下的行为都符合要求。
全覆盖式测试用例包含一整套单元测试,涵盖了各种可能的函数使用方式。
对于大型项目,要实现全覆盖可能很难。

11.1.2 可通过的测试

要为函数编写测试用例,可先导入模块unittest以及要测试的函数,在创建一个继承unittest.TestCase的类。,并编写一系列方法
对函数行为的不同方面进行测试。

11.1.3 不能通过的测试

11.1.4 测试未通过怎么办

11.1.5 添加新测试
11,2 测试类

前面是针对函数的测试,那么现在就是针对类的测试。
对类进行测试可以保证所做的改进没有破坏其原有的行为。

11.2.1 各种断言方法
unittest.TestCase类中提供了很多断言方法。断言方法检查你认为应该满足的条件是否确实满足。
如果该条件满足,你对程序行为的假设就得到了确认,你就可以确信其中没有错误。
反之,Python将发生异常。
unittest 中的断言方法

  1. assertEquals(a,b) 核实 a==b
  2. assertNotEquals(a,b) 核实 a != b
  3. assertTrue(x) 核实 x=True
    4.assertFalse(x) 核实 x=False
  4. assertIn(item, list) 核实item 在 list中。
  5. assertNotIn(item, list) 核实 item 不再 list中
    6月9号学习进度

1.Django-rest-fromwork遇到的问题

Django REST Framework (DRF): TypeError: register() got an unexpected keyword argument 'base_name’j
将base_name改为basename就好了

2.How to fix " AttributeError at /api/doc ‘AutoSchema’ object has no attribute ‘get_link’ " error in Django

在Settings.py下配置:

REST_FRAMEWORK = { ‘DEFAULT_SCHEMA_CLASS’: ‘rest_framework.schemas.coreapi.AutoSchema’ }
就好了。

第十六章——创建高级联结

1.使用表别名

select

concat(vend_name, ‘(’,vend_country, ‘)’)

as vend_title

from vendors

order by vend_name;

返回的数据:列别名

±-----------------------+
| vend_title             |
±-----------------------+
| ACME(USA)              |
| Anvils R Us(USA)       |
| Furball Inc.(USA)      |
| Jet Set(England)       |
| Jouets Et Ours(France) |
| LT Supplies(USA)       |
±-----------------------+

别名除了用于列名和计算字段外,SQL还允许给表名起别名。

这样做的理由是:缩短SQL语句; 允许在单条select语句中多次使用相同的表。

代码如下:

select cust_name, cust_contact

from customers as c, orders as o, orderitems as oi

where c.cust_id = o.cust_id

and o.order_num = oi.order_num

and prod_id = “TNT2”;

表别名:

返回的数据:±---------------±-------------+
| cust_name      | cust_contact |
±---------------±-------------+
| Coyote Inc.    | Y Lee        |
| Yosemite Place | Y Sam        |
±---------------±-------------+

可以看到,from子句中三个标全都具有别名。

表别名不仅能用于where子句,还可以用于select的列表,order by子句以及语句的其他部分。

注意:表别名只在查询执行中使用。与列别名不一样,表别名不返回客户机。

  1. 使用不同类型的联结

其他的联结:自联结、自然联结、外部联结

自联结:(了解)

select p1.prod_id, p1.prod_name

from products as p1, products as p2

where p1.vend_id = p2.vend_id

and p2.prod_id = “DTNTR”;

6月10号学习记录

1.flask遇到的问题

flask.cli.NoAppException: Could not import “hello”
在学习flask的时候,运行python -m flask run命令,启动程序报上面的问题

在要运行的py文件下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值