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; ...
随机推荐
- UI_DEV_Environment 之 StoryBook
写在前面 由于本文主要集中关注与工具使用,所以不可能完全介绍工具的所有功能,所以要想了解更多,可以自己去各自官方网站上查看. github examples 什么是UI开发环境 UI开发环境专注于用户 ...
- Linux中jar包启动和jar包后台运行
Linux 运行jar包命令如下: 方式一: java -jar shareniu.jar 特点:当前ssh窗口被锁定,可按CTRL + C打断程序运行,或直接关闭窗口,程序退出 那如何让窗口不锁定? ...
- find 常用命令
系统中总会不断产生一些文件,比如日志文件,不一定会用到也不会自动删除,这时候就需要手动删除,当然也可以转存到其他目录下.不好找的时候可以用find模糊查找,加个job定时任务自动执行定期删除文件1.添 ...
- MVC(实战一)
一.创建MVC项目 二.界面分布 Content:是存放css文件等,暂时先不考虑. Controllers:重要, 控制层,控制界面显示和界面逻辑的,其实真正业务逻辑层,建议分层出去. Models ...
- 埃氏筛法(求n以内有哪些个质数)
核心思想:从i=2开始,划去i的倍数,即剩下i为质数(如删去2的倍数后2为质数,再删去3的倍数后3为质数,4被删除则跳过,5未被删除则记录然后删除5的倍数...以此类推) #include <b ...
- 【SQL】多表查询中的 外连接 ,on,where
先简单粗暴给个结论,多表连结查询中,on比where更早起作用,系统首先根据各个表之间的联接条件,把多个表合成一个临时表后,再由where进行匹配过滤,where后语句为真,则能查询出来,而通过外连接 ...
- Python Django 支付宝 扫码支付
安装python-alipay-sdk pip install python-alipay-sdk --upgradepip install crypto 如果是python 2.7安装0.6.4这个 ...
- 使用python解析ip地址
前言 想要批量将ip地址转换为省份城市.国家或是经纬度?百度上的批量查找每次的容量太小满足不了要求?第三方库神器 - geoip2帮你解决所有烦恼. 准备工作 首先安装一下geoip2库, pip i ...
- .NET Core C# 中级篇2-7 文件操作
.NET Core CSharp 中级篇2-7 本节内容为文件操作 简介 文件操作在我们C#里还是比较常见的,例如我们读取Excel.Txt文件的内容,在程序中,这些文件都是以流的方式读取进入我们内存 ...
- 资深程序员对于Python各个方向的面试经验分享,非常给力!
之前早有前辈们说过,"裸辞一时爽,一直裸辞一直爽",这话一点不假,裸辞你要面临没有收入来源,但是每天眼睁睁看着各种花销不断支出的煎熬,我主要是觉得一边在上家公司工作一边去下家面试可 ...