0
点赞
收藏
分享

微信扫一扫

Python 中全局变量缓存的多线程问题及优化策略

一ke大白菜 04-11 11:00 阅读 0

一、检索一列或多列1.检索单独一列

select 列名 from 表名;

select order_num from orders;

2.检索多列数据

select 列 1,列 2... from 表名;

select order_num,order_date from orders; select order_date,order_num from orders;

3.查询所有字段

select * from 表名; select * from orders;

注:在生产环境中,坚决不允许使用 select *

二、去除查询结果中的重复值

1.select distinct 列 1,列 2... from 表名;

select distinct vend_id,prod_price from products;

注:distinct 关键对它后面跟的所有列都生效

三、使用 limit 子句控制显示结果条目数

1.select 列 1,列 2... from 表名 limit 需要显示的行数;

select prod_name from products limit 5;

2.select 列 1,列 2... from 表名 limit x,y;

注:1)x 是从第几行开始显示(包括 x),y 是显示的行数

  1. MariaDB 的行数是从第 0 行开始的

select prod_name from products limit 3,4;

  1. select 列 1,列 2... from 表名 limit 4 offset 3;

select prod_name from products limit 4 offset 3;

四、完全限定表名、列名

select 表名.列名 from 数据库名.表名;

 select orders.order_num from test.orders;

注:完全限定列名可以避免歧义(多个表中含有相同列)

完全限定表名可以实现跨数据库查询

五、注释三种形式:--、#、/* */

注:--后面与注释内容之间至少有一个空格

六、使用 order by 子句对查询结果进行排序(默认正序)desc降序

1.针对单独列进行排序 select 列名 from 表名 order by 列名按照此列进行排序;

select prod_name from products order by prod_name;

或 select 列名 1 from 表名 order by 列名 2;

select prod_name,prod_price from products order by prod_price;

注:通常 order by 子句中使用的列就是需要显示的列,然而用非检索的列排序也是完全合法的

2.针对多列进行排序按第一列排序如果第一列有重复指按第二列排

1)select 列 1,列 2 from 表名 order by 列 1,列 2;

select prod_id,prod_name,prod_price from products order by prod_price,prod_name;

2)select 列 1,列 2 from 表名 order by 1,2;按第1列排序,有重复按第2列排

select prod_id,prod_name,prod_price from products order by 3,2;

 3)降序排序(desc)

select 列 1,列 2 from 表名 order by 列 1 desc,列 2;

select prod_name,prod_price from products order by prod_price desc,prod_name;

注:desc 仅对其前面的列有效,其他后面没有 desc 的列仍然以正序排序

七、使用 order by 子句和 limit 子句显示最大/最小值1.显示最小值

select 列 1 from 表名 order by 列 1 limit 1;

select prod_price from products order by prod_price limit 1;

2.显示最大值 select 列 1 from 表名 order by 列 1 desc limit 1;

select prod_price from products order by prod_price desc limit 1;

八、where 子句

1.使用=操作符查询 select 列 1,列 2... from 表名 where 列 1='值';

select prod_name,prod_price from products where prod_name='fuses';

2.使用<操作符查询 select 列 1,列 2... from 表名 where 列 1<'值';

select prod_name,prod_price from products where prod_price < '10';

3.使用<>或!=操作符查询 select 列 1,列 2... from 表名 where 列 1<>'值';

select vend_id,prod_name from products where vend_id <> '1003';

select 列 1,列 2... from 表名 where 列 1!='值';

select vend_id,prod_name from products where vend_id != '1003';

 4.使用 between 操作符查询

select 列 1,列 2... from 表名 where 列 between 值 1 and 值 2;

select prod_name,prod_price from products where prod_price between 5 and 10;

注:between 和 and 为闭区间(即包含两边的值)

  1. 检索空值与非空值

1)检索空值

select 列 1,列 2... from 表名 where 列 is null;

select cust_id,cust_email from customers where cust_email is null;

2)检索非空值 select 列 1,列 2... from 表名 where 列 is not null;

select cust_id,cust_email from customers where cust_email is not null;

  1. 使用 and 和 or 操作符查询

1)使用 and 操作符(

select 列 1,列 2... from 表名 where 限定条件 1 and 限定条件 2 select vend_id,prod_id,prod_name,prod_price from products where vend_id=1003 and prod_price <=10;

 2)使用 or 操作符(或)

select 列 1,列 2... from 表名 where 限定条件 1 or 限定条件 2 select vend_id,prod_name,prod_price from products where vend_id=1002 or vend_id=1003;

3)and 要比 or 的优先级高

select vend_id,prod_name,prod_price from products where vend_id=1002 or vend_id=1003 and prod_price >= 10;

select vend_id,prod_name,prod_price from products where (vend_id=1002 or vend_id=1003) and prod_price >= 10;

7.使用 in 操作符查询

select 列 1,列 2 from 表名 where 列 in (值 1,值 2,值 3...); select vend_id,prod_name,prod_price from products where vend_id in (1002,1003);1002或1003

select vend_id,prod_name,prod_price from products where vend_id in (1002,1003) and prod_price >=10;

注:当你使用很长的值列表选项时,IN 操作符语法更清晰易读更容易管理优先级顺序比一系列 or 操作符执行的快可以创建较为动态的 where 子句

  1. 使用 not 操作符查询 select vend_id,prod_name,prod_price from products where vend_id not in (1002,1003);

  • 使用 like 操作符配合通配符查询

select 列 1,列 2 from 表名 where 列 like '关键字与通配符';

select prod_id,prod_name from products where prod_name like 'jet%';

select prod_id,prod_name from products where prod_name like '_ ton anvil';

%:匹配多个字符

—:匹配一个字符

十、使用 regexp 操作符配合正则表达式查询

select 列 1,列 2... from 表名 where 列 regexp '关键字与正则表达式';

1.使用.进行匹配匹配多个字符

select prod_id,prod_name from products where prod_name regexp '.000';

2.如果想要强制区分大小写,可以使用关键字 binary select prod_id,prod_name from products where prod_name regexp binary 'JetPack .000';

3.执行 or 匹配| 或

select prod_id,prod_name from products where prod_name regexp '1000|2000|3000';

  1. 使用字符集匹配 []从中选中一个

select prod_id,prod_name from products where prod_name regexp '[1235] ton';

5.匹配特殊字符“.”

select vend_name from vendors where vend_name regexp '\\.';

注:使用\\转义符,去掉了.的特殊含义(匹配任意一个字符)

  1. 使用元字符匹配查询 select prod_name from products where prod_name regexp '\\([0-9] sticks?\\)';TNT  (?匹配前一个字符0次或者多次

+ 匹配前一个字符一次或多次)

select prod_name from products where prod_name regexp '[[:digit:]]{4}';

注:上述 sql 语句等同于 select prod_name from products where prod_name regexp '[0-9][0-9][0-9][0-9]';

select prod_name from products where prod_name regexp '^[0-9\\.]'

十一、子查询

多个表间的 select 语句的嵌套查询例:检索所有订单包含物品 TNT2 的客户信息customers  orders  ordersitems

1.不使用子查询时

1)先查询所有包含物品 TNT2 的订单的订单号

select order_num,prod_id from orderitems where prod_id='TNT2';

2)再查询上述订单号的订单是哪个客户下的

select cust_id,order_num from orders where order_num in(20005,20007);

 3)最后查询上述客户的详细信息

select cust_id,cust_name,cust_contact from customers where cust_id in (10001,10004);

2.使用子查询(嵌套查询)

select cust_id,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'));

:子查询最多不超过 15 级,一般使用时不超过 2 级

大多数子查询都可以更改为连接查询

举报

相关推荐

0 条评论