How to use MySQL subquery as table in FROM clause?

WBOY
Release: 2023-08-27 21:53:02
forward
1151 people have browsed it

如何在 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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!