MySQL select from where multiple conditions
Maybe one of the most used MySQL commands is SELECT, that is the way to stract the information from the database, but of course one does not need all the info inside a database, therefore one should limit the info coming out from the table, there is WHERE statement comes into play, with it one can limit the data to only the one that complies with certain condition. What if that is still too wide?. Then it can be used multiple conditions. Here some options:
Working with two conditions
Using AND with two or more conditions the query can be narrowed to meet your needs.
SELECT * FROM table WHERE column1 = 'var1' AND column2 = 'var2';
Only when the two conditions are met the row is stracted from the database's table. What if any of them should be met to get the data?
SELECT * FROM table WHERE column1 = 'var1' OR column2 = 'var2';
Using OR will tell MySQL to return data if one or both conditions are met.
Working with more than two conditions
If more than two conditions need to be met in order to show a result, you need to use parenthesis and nest the conditions according to your needs. This time it will be easier with examples.
Consider this table:
+------------+-----------+--------+-----+
| fname | lname | gender | age |
+------------+-----------+--------+-----+
| John | Smith | M | 30 |
+------------+-----------+--------+-----+
| Jane | Doe | F | 20 |
+------------+-----------+--------+-----+
| Richard | Stallman | M | 70 |
+------------+-----------+--------+-----+
| John | Doe | M | 20 |
+------------+-----------+--------+-----+
If you want to get all young male's names use this query.
SELECT * FROM table WHERE gender = M AND age >= '18' AND age <= '50';
If you want to get all young people with last name Doe or Smith, use this query.
SELECT * FROM table WHERE age >= '18' AND age <= '50' AND (lname = 'Doe' OR lname = 'Smith');
As you can see we are using parenthesis to get a result from last names, because you want one or the other, then you use AND to get the age range and finally an AND to join the results from age range and the results from names.
SELECT unique_id,COUNT(unique_id)
FROM yourtblname
GROUP BY unique_id
HAVING COUNT(unique_id) >1
MySQL select from where multiple conditions的更多相关文章
- mysql select日期格式
mysql表中datatime类型存储为2016-01-10,C#直接select 后,在datatable里面看,变成01/10/2016,需要还原回去,使用select DATE_FORMAT(列 ...
- mysql select
select 查询: 赋值:赋值不能应用在where中,因为where操作的是磁盘上的文件,可以应用在having筛选中. 例:select (market_price-shop_price) as ...
- mysql select 格式化输出
select * from test\G; MySQL的客户端命令行工具,有很多方便使用者的特性,某些方面甚至可以说比Oracle的sqlplus更加人性化.当然从整体来说,还是sqlplus更加方便 ...
- mysql SELECT FOR UPDATE语句使用示例
以MySQL 的InnoDB 为例,预设的Tansaction isolation level 为REPEATABLE READ,在SELECT 的读取锁定主要分为两种方式:SELECT ... LO ...
- MySQL select into 和 SQL select into
现在有张表为student,我想将这个表里面的数据复制到一个为dust的新表中去,虽然可以用以下语句进行复制,总觉得不爽,希望各位帮助下我,谢谢. answer 01: create table d ...
- mysql SELECT FOUND_ROWS()与COUNT(*)用法区别
在mysql中 FOUND_ROWS()与COUNT(*)都可以统计记录,如果都一样为什么会有两个这样的函数呢,下面我来介绍SELECT FOUND_ROWS()与COUNT(*)用法区别 SEL ...
- php学习之道:mysql SELECT FOUND_ROWS()与COUNT(*)使用方法差别
在mysql中 FOUND_ROWS()与COUNT(*)都能够统计记录.假设都一样为什么会有两个这种函数呢.以下我来介绍SELECT FOUND_ROWS()与COUNT(*)使用方法差别 SELE ...
- mysql select column default value if is null
mysql select column default value if is null SELECT `w`.`city` AS `city`, `w`.`city_en` AS `city_en` ...
- MYSQL SELECT FOR UPDATE
问题说明: 最近遇到一个问题,多个WORKER同时向MYSQL数据库请求任务,如何实现互斥?例如: SELECT * FROM student WHERE id > 10 LIMIT 100; ...
随机推荐
- Parallel.ForEach 使用多线遍历循环
Parallel.ForEach相对于foreach是多线程,并行操作;foreach是单线程品德操作. static void Main(string[] args) { Console.Write ...
- angular cli 使用echarts
1.安装库 npm install typings echarts --global npm install ngx-echarts --save npm install @types/echarts ...
- AWS SNS 创建 订阅 发布
AWS SNS 创建 订阅 发布 20180810 chenxin 为实现短信报警,添加以下SNS的短信(SMS)订阅 选择主题,创建新主题,或修改原有主题 进入对应主题后,选择创建订阅,选择SMS, ...
- Where is the kernel documentation?; Ubuntu 上如何安装 linux 内核文档;fedora 上如何安装linux内核文档?
有时候,linux内核文档对我们很重要,我们可以在linux系统中安装,并及时查看: 参考链接:https://askubuntu.com/questions/841043/where-is-the- ...
- java执行-cp报错 error: could not load JDBC driver
首先查看对应的 jar 包是否存在,然后看一下 Server (获取数据库驱动类的名称 driverClassName)是否正确 例如: java -
- centos8 安装 nginx
http://nginx.org/ NGINX官网 创建文件夹mkdir nginx进入创建的文件夹 根据自己需要下载合适版本 通过 wget http://nginx.org/download/ng ...
- Security+学习笔记
第二章 风险分析 风险管理 评估:确定并评估系统中存在的风险 分析:分析风险对系统产生的潜在影响 响应:规划如何响应风险的策略 缓解: 缓解风险对未来安全造成的不良影响 风险分析流程 资产确定 漏洞确 ...
- 华为eNSP路由交换实验-生成树之RSTP
RSTP基础配置 实验拓扑图 实验步骤 1.基本配置 根据实验编址表进行相应的基本IP配置. 2.配置RSTP基本功能. (1)把生成树模式由默认的MSTP(华为交换机默认开启)改为RSTP. [FW ...
- 【模板】分治 FFT
Link Solution 有两种解法. 法1: 直接上分治FFT,也就是CDQ分治+FFT. 具体做法是先递归左半边,算出左半边答案之后,将左半边贡献到右半边,然后递归右半边. 分治是一个log的, ...
- vue axios 在 edge 浏览器下的bug
Edge 浏览器的版本: Microsoft Edge 42.17134.1.0Microsoft EdgeHTML 17.17134 当请求为POST 时,转换为 GET,并且始终报 “来自缓存 ...