数据库的简单查询

一、检索一列或多列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 级

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/595559.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

电工技术学习笔记——正弦交流电路

一、正弦交流电路 1. 正弦量的向量表示法 向量表示方法&#xff1a;正弦交流电路中&#xff0c;相量表示法是一种常用的方法&#xff0c;用于描述电压、电流及其相位关系。相量表示法将正弦交流信号表示为复数&#xff0c;通过复数的运算来描述电路中各种参数的相互关系 …

阿里云服务器2核2G优惠价格99元和61元一年配置对比

阿里云2核2G云服务器租用价格61元一年和99元1年&#xff1a;轻量应用服务器2核2G3M、50GB 高效云盘、3M带宽&#xff0c;不限制流量&#xff0c;优惠价格61元一年&#xff1b;云服务器ECS经济型e实例、2核2G&#xff0c;3M固定带宽&#xff0c;40G ESSD Entry云盘&#xff0c;租…

ChatGPT(3.5版本)开放无需注册:算力背后的数据之战悄然打响

✨✨ 欢迎大家来访Srlua的博文&#xff08;づ&#xffe3;3&#xffe3;&#xff09;づ╭❤&#xff5e;✨✨ &#x1f31f;&#x1f31f; 欢迎各位亲爱的读者&#xff0c;感谢你们抽出宝贵的时间来阅读我的文章。 我是Srlua小谢&#xff0c;在这里我会分享我的知识和经验。&am…

老板回来,我不知道--观察者模式

1.1 老板回来&#xff1f;我不知道 上班期间看股票行情&#xff0c;被老板当场看到。 "其实最近项目计划排得紧&#xff0c;是比较忙的。而最近的股市又特别的火&#xff0c;所以很多人都在偷偷地通过网页看行情。老板时常会出门办事&#xff0c;于是大家就可以轻松一些&a…

【Linux】Ubuntu 压缩与解压缩

首先在Windows下安装7Zip压缩软件&#xff0c;以便于可以生成 .tar 和 .bz2 的压缩格式的文件。例如新建一个test文件夹&#xff0c;操作后如下。 gzip 压缩工具&#xff1a;负责 .gz 格式的文件的压缩和解压缩&#xff0c;gzip --help 查看使用帮助&#xff1b; 压缩文件&…

9_springboot_shiro_jwt_多端认证鉴权_整合jwt

1. Shiro框架回顾 到目前为之&#xff0c;Shiro框架本身的知识点已经介绍完了。web环境下&#xff0c;整个框架从使用的角度我们需要关注的几个点&#xff1a; 要使用Shiro框架&#xff0c;就要创建核心部件securityManager 对象。 SpringBoot项目中&#xff0c;引入shiro-spr…

【WEEK6】 【DAY1】DQL查询数据-第一部分【中文版】

2024.4.1 Monday 目录 4.DQL查询数据&#xff08;重点&#xff01;&#xff09;4.1.Data Query Language查询数据语言4.2.SELECT4.2.1.语法4.2.2.实践4.2.2.1.查询字段 SELECT 字段/* FROM 表查询全部的某某查询指定字段 4.2.2.2.给查询结果或者查询的这个表起别名&#xff08…

吴恩达机器学习笔记:第 6 周-11机器学习系统的设计(Machine Learning System Design)11.1-11.5

目录 第 6 周 11、 机器学习系统的设计(Machine Learning System Design)11.1 首先要做什么11.2 误差分析11.3 类偏斜的误差度量11.4 查准率和查全率之间的权衡11.5 机器学习的数据 第 6 周 11、 机器学习系统的设计(Machine Learning System Design) 11.1 首先要做什么 在接…

使用oss对象存储服务的两种方式

1.用户访问应用服务器&#xff0c;应用服务器通过io流等方式存储文件到oss服务 2.但是上面要先经过应用服务器&#xff0c;一定程度上会有性能瓶颈&#xff0c;所以第二种&#xff0c;方式如下&#xff1a; 上传的时候先会去服务端要签名policy&#xff0c;然后要到签名数据后…

Echarts自定义折线图的节点与图标

文章目录 需求分析 需求 如图所示需要自定义节点的图标展示 分析 首先需要 symbol 属性添加引入 需要用到一个工具&#xff1a;http://tu.chacuo.net/imagetodataurl 这里是我的几个图片转出来的svg export const lineColors [#FF5360,#45CB85,#EEB902,#9CEC0F,#FF9562,#…

nest路由通配符使用

写法 路由路径 ‘ab*cd’ 将匹配 abcd 、ab_cd 、abecd 等。 src/cats/cats.controller.ts import { Controller, Get } from nestjs/common;Controller(cats) export class CatsController {Get(ab*cd)findAll1(): string {return 测试路由通配符;} }效果展示 截图1 截图2 …

电商系列之风控安全

> 插&#xff1a;AI时代&#xff0c;程序员或多或少要了解些人工智能&#xff0c;前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到网站。 坚持不懈&#xff0c;越努力越幸运&#xff0c;大家…