如何在單一的 MySQL 查詢中使用 id、名稱和年齡三個條件來獲取學生的記錄?
我們首先建立一個表 -
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(50), StudentAge int ); Query OK, 0 rows affected (0.72 sec)
使用 insert 命令向表中插入一些記錄 -
mysql> insert into DemoTable(StudentName,StudentAge) values('Chris',21);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable(StudentName,StudentAge) values('David',23);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(StudentName,StudentAge) values('Bob',22);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable(StudentName,StudentAge) values('Carol',21);
Query OK, 1 row affected (0.30 sec)使用 select 語句顯示錶中的所有記錄 -
mysql> select *from DemoTable;
這將產生以下輸出 &minusl;
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | Chris | 21 | | 2 | David | 23 | | 3 | Bob | 22 | | 4 | Carol | 21 | +-----------+-------------+------------+ 4 rows in set (0.00 sec)
以下查詢用於實現三個條件來獲取特定記錄 -
mysql> select *from DemoTable where StudentId=4 and StudentName='Carol' and StudentAge=21;
這將產生以下輸出 -
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 4 | Carol | 21 | +-----------+-------------+------------+ 1 row in set (0.00 sec)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP