MySQL 检索数据总结
时间:2023-01-13
浏览:412
作者:佚名
编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《MySQL 检索数据总结》,文章讲解的知识点主要包括MySQL,如果你对数据库方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。
1. 基本查询
// 检索单个列 select prod_name from products; // 检索所有列 select * from products; // 检索不同的行 select distinct vend_id from products; // 限制结果 select prod_name from products limit 4,5;
1.// 对 `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.过滤数据
使用
// 检查单个值,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.在同时使用// `%` 通配符 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.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 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.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;保证子查询中的// 等值联接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.应该保证所有联接都// 返回价格小于等于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语句之后。好了,本文到此结束,带大家了解了《MySQL 检索数据总结》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多数据库知识!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
