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; ...
随机推荐
- 记,NSProxy需要实现哪些方法?
转注出:https://www.cnblogs.com/xiaobajiu/p/10799962.html 使用NSProxy做替身,代理,多继承,本质上都是用它来转发消息给真身. 观察头文件,NSP ...
- python部署mariadb主从架构
主机部署: import configparser import os def config_mariadb_yum(): exists = os.path.exists('/etc/yum.repo ...
- 《收获,不止SQL优化》这本书,有很多即用的脚本工具,或者根据自己的需求,改造重用,可以积累到自己的工具库中。
以下两个脚本,官方来源: https://github.com/liangjingbin99/shouhuo/tree/master/%E7%AC%AC05%E7%AB%A0 1. 找出未使用绑定变量 ...
- pycharm报错:Process finished with exit code -1073741819 (0xC0000005)解决办法
这个是几个月前的问题了,有小伙伴在CSDN问我咋解决的,那我今天在这边把这个问题解决办法分享下吧,免得大家把很多时间都浪费在安装排坑上面,有些坑虽然解决了还真不知道啥原因. 我的pycharm一直用的 ...
- Excel上下标如何设置?
Excel表格怎么设置上下标?Excel上下标设置技巧 在21世纪的我们,平时的工作和学习中,经常会使用到一些专业的文档,比如方程式.数据的公式和科学计数等,其中均会涉及到许多的上下标符号输入以及使用 ...
- redis实现分布式锁--工具类
1.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Java基础(七)
左连接,右连接,内连接,全连接的区别 左连接:返回左表所有行,右表没有匹配行则返回null 右连接:返回右表所有行,左表没有匹配行则返回null 内连接:返回左右表共有行 全连接:返回左右表所有行,无 ...
- sublime插件开发教程3
今天就看下api的使用方法 中文的api文档 https://mux.alimama.com/posts/549.html#sublime.View sublime模块 方法 返回值 描述 set_t ...
- 测试:Oracle 19c RAC添加私网
最近有个客户需求是在某12.2版本的RAC环境上添加心跳网络,顺便考虑将之前的心跳网络改为asm专用.我目前只有19c的RAC的测试环境(19c是12c的最终稳定版本),直接测试验证下过程备忘. 1. ...
- Excel导入导出DataGridView
/// <summary> /// excel表保存到dataTable中 /// </summary> /// <param name="path" ...