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的更多相关文章

  1. mysql select日期格式

    mysql表中datatime类型存储为2016-01-10,C#直接select 后,在datatable里面看,变成01/10/2016,需要还原回去,使用select DATE_FORMAT(列 ...

  2. mysql select

    select 查询: 赋值:赋值不能应用在where中,因为where操作的是磁盘上的文件,可以应用在having筛选中. 例:select (market_price-shop_price) as ...

  3. mysql select 格式化输出

    select * from test\G; MySQL的客户端命令行工具,有很多方便使用者的特性,某些方面甚至可以说比Oracle的sqlplus更加人性化.当然从整体来说,还是sqlplus更加方便 ...

  4. mysql SELECT FOR UPDATE语句使用示例

    以MySQL 的InnoDB 为例,预设的Tansaction isolation level 为REPEATABLE READ,在SELECT 的读取锁定主要分为两种方式:SELECT ... LO ...

  5. MySQL select into 和 SQL select into

    现在有张表为student,我想将这个表里面的数据复制到一个为dust的新表中去,虽然可以用以下语句进行复制,总觉得不爽,希望各位帮助下我,谢谢.  answer 01: create table d ...

  6. mysql SELECT FOUND_ROWS()与COUNT(*)用法区别

    在mysql中 FOUND_ROWS()与COUNT(*)都可以统计记录,如果都一样为什么会有两个这样的函数呢,下面我来介绍SELECT FOUND_ROWS()与COUNT(*)用法区别   SEL ...

  7. php学习之道:mysql SELECT FOUND_ROWS()与COUNT(*)使用方法差别

    在mysql中 FOUND_ROWS()与COUNT(*)都能够统计记录.假设都一样为什么会有两个这种函数呢.以下我来介绍SELECT FOUND_ROWS()与COUNT(*)使用方法差别 SELE ...

  8. 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` ...

  9. MYSQL SELECT FOR UPDATE

    问题说明: 最近遇到一个问题,多个WORKER同时向MYSQL数据库请求任务,如何实现互斥?例如: SELECT * FROM student WHERE id > 10 LIMIT 100; ...

随机推荐

  1. 1.Ansible自动化管理工具

    1.Ansible基本概述 Ansible是一个IT自动化的配置管理工具,自动化主要体现在Ansible集成了丰富模块,丰富的功能组件, 可以通过一个命令行完成一系列的操作.进而能减少我们重复性的工作 ...

  2. Refresh design pattern

    多年后, 再次翻阅设计模式书籍, 将每种模式的要点总结于此, 需要本身有一定设计模式基础, 再结合要点, 帮助更好理解与运用. 策略模式 (Strategy) 附加功能单独抽象不同接口 功能以组合方式 ...

  3. create connection SQLException, url: jdbc:mysql://localhost:3306/demo, errorCode 1045, state 28000

    错误原因: 配置文件中 username 与 Mysql 关键字冲突 改为:

  4. js执行机制

    js是单线程的,为什么可以执行异步操作呢? 这归结与浏览器(js的宿主环境)通过某种方式使得js具备了异步的属性. 区分进程和线程: 进程:正在运行中的应用程序.每个进程都自己独立的内存空间.例如:打 ...

  5. diango下载、创建、启动

    下载 命令行 pip install django==1.11.26 -i https://pypi.tuna.tsinghua.edu.cn/simple pycharm 创建项目 命令行 djan ...

  6. Mysql增量备份之Mysqldump&Mylvmbackup

    简单介绍 备份类型 备份方式 热备份:备份期间不需要服务停机,业务不受影响: 温备份:备份期间仅允许读的请求: 冷备份:备份期间需要关闭Mysql服务或读写请求都不受影响: 完全备份:full bac ...

  7. IDEA 工具自动生成JavaBean类

    1.先安装GsonFormat插件:File-->Setting-->Plugins-->GsonFormat-->OK 2.new 一个新的Class空文件,然后 Alt+I ...

  8. react官方脚手架添加less配置

    装两个包 npm install --save less less-loader 在node-modules/react-scripts/config/webpack.config.js中 在大概58 ...

  9. 五、如何通过CT三维图像得到DRR图像

    一.介绍 获取DRR图像是医疗图像配准里面的一个重要的前置步骤:它的主要目的是,通过CT三维图像,获取模拟X射线影像,这个过程也被称为数字影响重建. 在2D/3D的配准流程里面,需要首先通过CT三维图 ...

  10. Java描述设计模式(10):组合模式

    本文源码:GitHub·点这里 || GitEE·点这里 一.生活场景 1.文件系统 下图是常见的计算机文件系统的一部分. 文件系统是一个树结构,树上长有节点.树的节点有两种: 树枝节点 即文件夹,有 ...