SQL 基础查询和条件查询
select&from
用户信息表中的所有数据
id | device_id | gender | age | university | province |
1 | 2138 | male | 21 | 北京大学 | Beijing |
2 | 3214 | male | 复旦大学 | Shanghai | |
3 | 6543 | female | 20 | 北京大学 | Beijing |
4 | 2315 | female | 23 | 浙江大学 | ZheJiang |
5 | 5432 | male | 25 | 山东大学 | Shandong |
查询表中的所有数据
select *
from user_profile
返回结果:
id | device_id | gender | age | university | province |
1 | 2138 | male | 21 | 北京大学 | Beijing |
2 | 3214 | male | 复旦大学 | Shanghai | |
3 | 6543 | female | 20 | 北京大学 | Beijing |
4 | 2315 | female | 23 | 浙江大学 | Zhejiang |
5 | 5432 | male | 25 | 山东大学 | Shandong |
查询多列数据:用户的设备id对应的性别、年龄和学校的数据
select device_id,gender,age,university
from user_profile
返回结果:
device_id | gender | age | university |
2138 | male | 21 | 北京大学 |
3214 | male | 复旦大学 | |
6543 | female | 20 | 北京大学 |
2315 | female | 23 | 浙江大学 |
5432 | male | 25 | 山东大学 |
查询结果去重:从用户信息表中取出学校的去重数据。
select distinct university
from user_profile
返回结果:
university |
北京大学 |
复旦大学 |
浙江大学 |
山东大学 |
查询结果限制返回行数:查看前2个用户明细设备ID数据
select device_id
from user_profile
limit 2
返回结果:
device_id |
2138 |
3214 |
将查询后的列重新命名:查看前2个用户明细设备ID数据,并将列名改为 'user_infos_example'
select device_id as user_infos_example
from user_profile
limit 2
返回结果:
user_infos_example |
2138 |
3214 |
总结:
- 基础语法
- select 字段名 from 表名称
- 别名语法
- select 字段名 as 别名 from 表名称
- 注意:as可以省略
- 查询多列
- select 字段名1, 字段名2, 字段名3 from 表名称
- 查询所有列
- select * from 表名称
- 数据去重
- select distinct 字段名 from 表名称
- select中的计算字段
- select 字段名,计算字段 from 表名称
- 注意:计算字段中的算式所涉及的 字段名必须是表格中包含的,或者算式本身可以独立运算
where
筛选出所有北京大学的学生
select device_id,university
from user_profile
where university='北京大学'
返回结果:
device_id | university |
2138 | 北京大学 |
6543 | 北京大学 |
查找年龄大于24岁的用户信息(设备ID、性别、年龄、学校)
select device_id,gender,age,university
from user_profile
where age>24
返回结果:
device_id | gender | age | university |
5432 | male | 25 | 山东大学 |
查找20岁及以上且23岁及以下的用户信息
select device_id,gender,age
from user_profile
where age between 20 and 23
返回结果:
device_id | gender | age |
2138 | male | 21 |
6543 | female | 20 |
2315 | female | 23 |
查看除复旦大学以外的所有用户明细
select device_id,gender,age,university
from user_profile
where university!='复旦大学'
返回结果:
device_id | gender | age | university |
2138 | male | 21 | 北京大学 |
6543 | female | 20 | 北京大学 |
2315 | female | 23 | 浙江大学 |
5432 | male | 25 | 山东大学 |
取出所有年龄值不为空的用户的设备ID,性别,年龄,学校的信息
select device_id,gender,age,university
from user_profile
where age is not null
返回结果:
device_id | gender | age | university |
2138 | male | 21 | 北京大学 |
6543 | female | 20 | 北京大学 |
2315 | female | 23 | 浙江大学 |
5432 | male | 25 | 山东大学 |
user_profile
id | device_id | gender | age | university | gpa |
1 | 2138 | male | 21 | 北京大学 | 3.4 |
2 | 3214 | male | 复旦大学 | 4.0 | |
3 | 6543 | female | 20 | 北京大学 | 3.2 |
4 | 2315 | female | 23 | 浙江大学 | 3.6 |
5 | 5432 | male | 25 | 山东大学 | 3.8 |
找到男性且GPA在3.5以上(不包括3.5)的用户
select device_id,gender,age,university,gpa
from user_profile
where gender='male' and gpa>3.5
学校为北大或GPA在3.7以上(不包括3.7)的用户
select device_id,gender,age,university,gpa
from user_profile
where university='北京大学' or gpa>3.7
学校为北大、复旦和山大的同学
select device_id,gender,age,university,gpa
from user_profile
where university in ('北京大学','复旦大学','山东大学')
gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学
select device_id,gender,age,university,gpa
from user_profile
where (gpa>3.5 and university='山东大学') or (gpa>3.8 and university='复旦大学')
查看所有大学中带有北京的用户的信息
select device_id,age,university
from user_profile
where university like '%北京%'
%代表一个或多个字符的通配符,_代表仅仅一个字符的通配符
总结:
- 标准语法
- select 字段名
- from 表格名
- where 条件代码
- 运算符查询语法
- select 字段名
- from 表名称
- where 字段名 运算符 值
- 模糊查询语法
- select 字段名
- from 表名称
- where 字段名 like '通配符+字符'
- 使用多条件查询
- select 字段名
- from 表名称
- where 条件代码1 and|or 条件代码2
- 标准语法
- select 字段名
- from 表格名
- where 条件代码
- 运算符查询语法
- select 字段名
- from 表名称
- where 字段名 运算符 值
- 模糊查询语法
- select 字段名
- from 表名称
- where 字段名 like '通配符+字符'
- 使用多条件查询
- select 字段名
- from 表名称
- where 条件代码1 and|or 条件代码2
order by
ORDER BY 子句取一个或多个列的名字,据此对输出进行排序
取出用户信息表中的用户年龄,并按照年龄升序排序
select device_id,age
from user_profile
order by age
返回结果:
device_id | age |
6534 | 20 |
2138 | 21 |
3214 | 23 |
2315 | 23 |
5432 | 25 |
2131 | 28 |
默认order by 为升序排序
先按照gpa升序排序,再按照年龄升序排序
select device_id,gpa,age
from user_profile
order by gpa,age
按照gpa、年龄降序排序
select device_id,gpa,age
from user_profile
order by gpa desc,age desc
SQL 基础查询和条件查询相关推荐
- MySQL基础(八):模糊查询的SQL语句、where条件查询、比较运算符查询、逻辑运算符查询、模糊查询、范围查询、空判断查询
文章目录 where条件查询 1. where条件查询的介绍 2. 比较运算符查询 3. 逻辑运算符查询 4. 模糊查询 5. 范围查询 6. 空判断查询 7. 小结 where条件查询 学习目标 能 ...
- SQL 中的多条件查询
在应用程序开发中,多条件查询是个经常遇到的情况,最简单最麻烦的方法是把所有的可能情况都考虑到,但是无疑是繁琐的,而且很容易漏掉可能的情形,下面是SQL语句实现多条件查询的情况 select * fro ...
- 1.实现sql语言中DQL语言,实现简单查询、条件查询、排序查询等。
1.实现sql语言中DQL语言,实现简单查询.条件查询.排序查询等. (1)检索grade表当中的所有信息. (2)检索grade表数学成绩大于100的所有人信息. <%@ page langu ...
- NHibernate之旅(4):探索查询之条件查询(Criteria Query)
本节内容 NHibernate中的查询方法 条件查询(Criteria Query) 1.创建ICriteria实例 2.结果集限制 3.结果集排序 4.一些说明 根据示例查询(Query By Ex ...
- MySQL 基本查询、条件查询、投影查询
文章目录 1. 基本查询 2. 条件查询 3. 投影查询 练习 LeetCode 595. 大的国家 练习 LeetCode 584. 寻找用户推荐人 练习 LeetCode 1173. 即时食物配送 ...
- MySQL查询之条件查询
前言 之前总结了MySQL查询中的 基础查询 接下来将在本篇进行MySQL中的条件查询的总结 条件查询语句 条件查询就是在基础查询的基础之上,添加判定条件,对数据进行筛选,查找到相对应的数据 这时我们 ...
- 【示例】solr添加文档/删除文档/简单查询/多条件查询
solr添加文档/删除文档/简单查询/多条件查询
- Elasticsearch 入门(1):基本概念,安装教程,索引的创建,查询,删除,主键查询,修改,添加,聚合查询,条件查询
Elasticsearch 入门 基本概念 The Elastic Stack, 包括 Elasticsearch.Kibana.Beats 和 Logstash(也称为 ELK Stack).能够安 ...
- Mybatis-Plus查询之条件查询(用户名查询)(批量查询)
Mybatis-Plus查询之条件查询(用户名查询)(批量查询) 上代码 上代码 //查询多个用户//Arrays.asList 工具类List<User> users = iUserRe ...
- SQL数据库语言基础之SqlServer条件查询、排序数据表、like模糊查询【大总结】
文章目录 一.语句查询数据(SELECT) 二.获取满足查询条件的语句(WHERE) 三.排序查询(ORDER BY) 四.高级条件查询 一.语句查询数据(SELECT) 1.在SSMS中查看数据 在 ...
最新文章
- 精准广告系统架构调研
- 自动驾驶中,激光雷达点云如何做特征表达
- Annoy搜索算法(Approximate Nearest Neighbors Oh Yeah)
- python threading多线程计算
- 10-10数组的介绍
- Java String compareTo()方法与示例
- centos mysql自动备份_CentOS下每天自动备份mysql数据库
- 肿瘤化疗无效是对预先存在的突变的选择还是诱发新突变,Cell给你答案
- 坐拥12亿月活流量,腾讯直播带货的底牌
- 设计模式系列--Observer
- 管理者要会讲的六十八个故事
- PLSQL使用ODBC导入EXCEL时DSN无下拉选项
- 电子电路基础——知识点(下篇)
- ligerui 用html 来初始化 ligerform
- mg动画制作的简单思路整理
- 如何将扫描PDF文件转换成word,两个超简单的方法一看就会
- Kotlin - 改良构建者模式
- 修改hosts不管用。为什么修改127.0.0.1指向的域名,访问域名却弹出别的网站
- 如何压缩图片200k以下?
- 为什么VR依然没有出现烧钱向的3A级游戏?
热门文章
- OS X 自动关机设置
- 伽玛函数_gamma
- babel.config.js 和 .babelrc
- java color 半透明_Plotly的fillcolor默认为半透明,不需要透明度
- jfinal ajax传值,JFINAL+Ajax传参 array 数组方法 获取request中数组操作
- UpdateData(TRUE)和UpdateData(FALSE)的区别
- arp嗅探——ettercap+driftnet
- 互联网金融领域反欺诈技术已发展到哪一步?
- 远程管理服务SSH配置文件说明
- oracle层级计算推演,R语言使用层次分析法进行综合指标等级划分