使用 AND 和 OR 運算子使用單次 MySQL 查詢獲取特定記錄
首先建立一個表 −
mysql> create table DemoTable2015 -> ( -> StudentId int, -> StudentName varchar(20), -> StudentCountryName varchar(20) -> ); Query OK, 0 rows affected (1.20 sec)
使用 insert 命令在表中插入一些記錄 −
mysql> insert into DemoTable2015 values(1,'Chris','US'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable2015 values(2,'Bob','UK'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2015 values(3,'David','AUS'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable2015 values(4,'Robert','US'); Query OK, 1 row affected (0.16 sec)
使用 select 語句顯示錶中的所有記錄 −
mysql> select *from DemoTable2015;
這將生成以下輸出 −
+-----------+-------------+--------------------+ | StudentId | StudentName | StudentCountryName | +-----------+-------------+--------------------+ | 1 | Chris | US | | 2 | Bob | UK | | 3 | David | AUS | | 4 | Robert | US | +-----------+-------------+--------------------+ 4 rows in set (0.00 sec)
以下是一個查詢,可透過單個查詢獲取特定記錄 −
mysql> select *from DemoTable2015 -> where StudentName='David' OR StudentCountryName='UK' and StudentId=3;
這將生成以下輸出 −
+-----------+-------------+--------------------+ | StudentId | StudentName | StudentCountryName | +-----------+-------------+--------------------+ | 3 | David | AUS | +-----------+-------------+--------------------+ 1 row in set (0.00 sec)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP