在 MySQL 中針對一個別名執行過濾操作?
為此,在 HAVING 子句中使用別名。
我們首先建立一個表 −
mysql> create table DemoTable755 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Score1 int, Score2 int ); Query OK, 0 rows affected (0.62 sec)
使用 insert 命令插入一些記錄到表中 −
mysql> insert into DemoTable755(Score1,Score2) values(30,23); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable755(Score1,Score2) values(50,60); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable755(Score1,Score2) values(89,90); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable755(Score1,Score2) values(99,99); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable755(Score1,Score2) values(40,43); Query OK, 1 row affected (0.14 sec)
使用 select 語句顯示錶中的所有記錄 −
mysql> select *from DemoTable755;
這將產生以下輸出 -
+----+--------+--------+ | Id | Score1 | Score2 | +----+--------+--------+ | 1 | 30 | 23 | | 2 | 50 | 60 | | 3 | 89 | 90 | | 4 | 99 | 99 | | 5 | 40 | 43 | +----+--------+--------+ 5 rows in set (0.00 sec)
以下是針對 MySQL 中的別名執行過濾操作的查詢。在此,我們正在新增分數並在 HAVING 子句的查詢中顯示匹配結果。過濾記錄顯示 ID 以及帶有分數的結果 −
mysql> select Score1+Score2 AS Result,DemoTable755.* from DemoTable755 having Result=83;
這將產生以下輸出 -
+--------+----+--------+--------+ | Result | Id | Score1 | Score2 | +--------+----+--------+--------+ | 83 | 5 | 40 | 43 | +--------+----+--------+--------+ 1 row in set (0.00 sec)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP