Home  >  Article  >  Database  >  How to use MySQL subquery as table in FROM clause?

How to use MySQL subquery as table in FROM clause?

WBOY
WBOYforward
2023-08-27 21:53:021218browse

如何在 FROM 子句中使用 MySQL 子查询作为表?

We can use the subquery as a table in the FROM clause just like we use the results and operators of the subquery in the WHERE clause. In the following example, we use it as a table by writing the results of the subquery after the FROM clause. It is mandatory to use alias after subquery, here we use alias 'C_car'. For demonstration we have used the following data from table 'Cars' -

mysql> Select * from Cars;
+------+--------------+---------+
| ID   | Name         | Price   |
+------+--------------+---------+
|    1 | Nexa         | 750000  |
|    2 | Maruti Swift | 450000  |
|    3 | BMW          | 4450000 |
|    4 | VOLVO        | 2250000 |
|    5 | Alto         | 250000  |
|    6 | Skoda        | 1250000 |
|    7 | Toyota       | 2400000 |
|    8 | Ford         | 1100000 |
+------+--------------+---------+
8 rows in set (0.02 sec)

mysql> Select * FROM ( Select Name, Price from Cars Where Price > 200000) C_car WHERE Name = 'Nexa';
+------+--------+
| Name | Price  |
+------+--------+
| Nexa | 750000 |
+------+--------+
1 row in set (0.00 sec)

The above is the detailed content of How to use MySQL subquery as table in FROM clause?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete