数据库-多条件查询-优先级
生活随笔
收集整理的這篇文章主要介紹了
数据库-多条件查询-优先级
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
多條件查詢(and | or)?
mysql> SELECT * FROM pet WHERE species = 'dog' AND sex = 'f'; +‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+ | name | owner | species | sex | birth | death | +‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+ | Buffy | Harold | dog | f | 1989‐05‐13 | NULL | +‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+ mysql> SELECT * FROM pet WHERE species = 'snake' OR species = 'bird'; +‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+ | name | owner | species | sex | birth | death | +‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+ | Chirpy | Gwen | bird | f | 1998‐09‐11 | NULL | | Whistler | Gwen | bird | NULL | 1997‐12‐09 | NULL | | Slim | Benny | snake | m | 1996‐04‐29 | NULL | +‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+優先執行括號中的邏輯
mysql> SELECT * FROM pet WHERE (species = 'cat' AND sex = 'm') ‐> OR (species = 'dog' AND sex = 'f'); +‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+ | name | owner | species | sex | birth | death | +‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+ | Claws | Gwen | cat | m | 1994‐03‐17 | NULL | | Buffy | Harold | dog | f | 1989‐05‐13 | NULL | +‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+檢索特定的列
mysql> SELECT name, birth FROM pet; +‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+ | name | birth | +‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+ | Fluffy | 1993‐02‐04 | | Claws | 1994‐03‐17 | | Buffy | 1989‐05‐13 | | Fang | 1990‐08‐27 | | Bowser | 1989‐08‐31 | | Chirpy | 1998‐09‐11 | | Whistler | 1997‐12‐09 | | Slim | 1996‐04‐29 | | Puffball | 1999‐03‐30 | +‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+查詢不重復的字段要使用關鍵詞DISTINCT?
mysql> SELECT DISTINCT owner FROM pet; +‐‐‐‐‐‐‐‐+ | owner | +‐‐‐‐‐‐‐‐+ | Benny | | Diane | | Gwen | | Harold | +‐‐‐‐‐‐‐‐+可以使用組合條件查詢特定的列
mysql> SELECT name, species, birth FROM pet ‐> WHERE species = 'dog' OR species = 'cat'; +‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+ | name | species | birth | +‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+ | Fluffy | cat | 1993‐02‐04 | | Claws | cat | 1994‐03‐17 | | Buffy | dog | 1989‐05‐13 | | Fang | dog | 1990‐08‐27 | | Bowser | dog | 1989‐08‐31 | +‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+?
總結
以上是生活随笔為你收集整理的数据库-多条件查询-优先级的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库-检索部分-查找-更新表记录
- 下一篇: 数据库-排序-升降序-多列