MySQL 检索数据总结

1. 基本查询

// 检索单个列
select prod_name from products; 

// 检索全部列
select * from products; 

// 检索不一样的行
select distinct vend_id from products; 

// 限制结果
select prod_name from products limit 4,5;
1. LIMIT :如有两个参数,则第一个参数为开始位置(从 0 行开始),第二个参数为返回结果的数量;若只有一个参数,则表示限制返回结果的数量。
2.除非确实须要表中的每一个列,不然最好别使用 * 通配符,检索不须要的列会下降检索的性能。

2. 排序数据

// 对 `prod_name` 进行升序排序
select prod_name from products order by prod_name; 

//  按多个列排序: 对 `prod_price` 降序排序,对 `prod_name` 升序排序 
select prod_price, prod_name 
form products 
order by prod_price desc, prod_name;
若是没有明确规定排序顺序,MySQL 默认以数据添加到表中的顺序返回。

3.过滤数据

使用 WHERE 子句来指定搜索条件。mysql

// 检查单个值,MySQL 在执行匹配时默认不区分大小写,所以 'jochen' 和 'Jochen' 都会匹配
select prod_name, prod_price from products where prod_name = 'jochen'; 

// 不匹配检查
select vend_id, prod_name from products where vend_id != 1000; 

// 范围检查
select prod_name, prod_price 
from products 
where prod_price between 5 and 10; 

// 空值检查
select cust_id from customers where cust_email is null; 

// AND 操做符
select prod_id, prod_price 
from products 
where vend_id = 1003 and prod_price <=10; 

// OR 操做符
select prod_name, prod_price 
from products 
where vend_id = 1002 or vend_id = 1003; 

// IN 操做符
select prod_name, prod_price 
from products 
where vend_id in (1002, 1003); 

// NOT 操做符
select prod_name, prod_price 
from products 
where vend_id not in (1002, 1003);
1.在同时使用 ORDER BYWHERE 语句时,应该让 ORDER BY 位于 WHERE 以后,不然会报错
2.在 MySQL 中, AND 操做符的计算次序优先级高于 OR;可使用圆括号 ()提升操做符的优先级。

4. 通配符过滤

MySQL 中有两种通配符来实现匹配值的一部分的特殊字符:git

// `%` 通配符
select prod_id, prod_name from products where prod_name like 'jo%'; 

// `_` 通配符
select prod_id, prod_name from products where prod_name like '_ochen';
1. % 通配符表示任意字符出现任意次数; _ 通配符表示只匹配单个字符。
2.通配符搜索的处理通常要比普通搜索所花时间更长,所以不要过分使用通配符。

5. 正则表达式检索

正则表达式是用来匹配文本的特殊的串,经过使用 REGEXP 子句来实现。MySQL 中使用 POSIX 规范正则表达式。正则表达式

select prod_name 
from products 
where prod_name regexp '1000|2000';

select prod_name 
from products 
where prod_name regexp '[123] Ton';

// 连续4位数字
select prod_name 
from products 
where prod_name regexp '[[:digit:]]{4}';

6. 计算字段

应用程序所须要的数据每每并不存在于数据库表中,咱们须要从数据库中检索并进行拼接、计算、转换或者格式化等处理而获得,这就是计算字段。计算字段并不实际存在于数据库表中,而是运行时由 SELECT 语句建立。sql

// 拼接
select concat(vend_name, '(', vend_country, ')') as vend_title from vendors; 

// 算术计算
select prod_id, quantity, item_price quantity*item_price as expanded_price from orderitems;

7. 分组数据

分组容许把数据分为多个逻辑组,以便对每一个组进行汇集计算。数据库

// 返回每一个供应商提供的产品数目
select vend_id, count(*) as num_prods 
from products 
group by vend_id; # 建立分组

// 返回至少有两个订单的全部顾客
select cust_id, count(*) as orders 
from orders 
group by cust_id 
having count(*) >= 2; # 过滤分组

// 返回总计订单价格大于等于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;
1. GROUP BY 子句指示 MySQL 分组数据,而后对每一个组而不是整个结果集进行汇集。
2. GROUP BY 子句中列出的每一个列必须是检索列或者有效的表达式(不能是汇集函数),同时不能使用别名。
3. HAVING 在数据分组以后进行过滤, WHERE 在数据分组以前进行过滤。
4.通常在使用 GROUP BY 子句时,应该也给好 'ORDER BY' 子句,这是保证数据正确排序的惟一方法。

8. 子查询

MySQL 容许建立子查询,即嵌套在其余查询中的查询,例如把一条 SELECT 语句返回的结果用于另外一条 SELECT 语句的 WHERE 子句。函数

select cust_id 
from orders 
where order_num in (select order_num from ordreitems where prod_id = 'TNT2');

// 做为计算字段使用子查询
select cust_name, cust_state, (select count(*) 
from orders 
where orders.cust_id = customers.cust_id) as orders from customers 
order by cust_name;
保证子查询中的 SELECT 语句具备与父查询中的 WHERE 子句有相同数目的列。

9. 联接表

关系表的设计就是要保证把信息分解成多个表,一类数据一个表,各表经过某些经常使用的值(即关系设计中的关系)互相关联。分解数据为多个表能更有效地存储,更方便地处理,而且具备更大的可伸缩性。若是数据存储在多个表中,怎样用单条 SELECT 语句检索出数据?答案是使用联接。简单地说,联接是一种机制,用来在一条 SELECT 语句中关联表,使用特殊的语法,能够联接多个表返回一组数据。性能

联接不是物理实体,它在实际的数据库表中不存在。

经常使用的联接类型有:设计

  • 内部联接(INNER JOIN):两表执行笛卡尔积后,取列值符合查询条件的数据。
  • 左外部联接(LEFT OUTER JOIN):指将左表的全部记录与右表符合条件的记录,返回的结果除内链接的结果,还有左表不符合条件的记录,并在右表相应列中填NULL。
  • 右外部联接(RIGHT OUTER JOIN):
// 等值联接1
select vend_name, prod_name, prod_price 
from vendors, products 
where vendors.id = products.vend_id 
order by vend_name, prod_name;

// 等值联接2
select vend_name, prod_name, prod_price 
from vendors 
inner join products on vendors.id = products.vend_id 
order by vend_name, prod_name;
1.应该保证全部联接都 SELECT 子句,不然 MySQL 将返回比想要的数据多得多的数据(笛卡尔积)。
2.MySQL 在运行时关联指定的每一个表以及处理联接,这种处理可能时很是耗费资源的。

10. 复合查询

MySQL 容许执行多个查询(多条 SELECT 语句),并将结果做为单个查询结果集返回,这些组合查询称为复合查询。有两种状况下,须要使用复合查询:code

  • 在单个查询中从不一样的表返回相似结果的数据;
  • 对单个表执行多个查询,按单个查询返回数据。
// 返回价格小于等于5的全部物品、而且包括供应商1001和1002生产的全部物品
select vend_id, prod_id, prod_price
from products
where prod_price <=5
union
select vend_id, prod_id, prod_price
from products
where vend_id in (1001, 1002);
1. UNION 中的每一个查询必须包含相同的列、表达式或汇集函数。
2.在使用 UNION 复合查询是,只能使用一条 ORDER BY 子句,且必须在最后一条 SELECT 语句以后。
相关文章
相关标签/搜索