MySQL has an interesting feature called subquery. It lets you to use SELECT statement within another query.
Imagine you have two tables named countries and capitals you want select capital names and country names without join statement.
The tables are as follow:
mysql> SELECT * FROM countries; +------+------------+ | code | name | +------+------------+ | 1 | Azerbaijan | | 2 | Japan | | 3 | Germany | | 4 | USA | | 6 | Turkey | +------+------------+ mysql> SELECT * FROM capitals; +---------+------------+ | country | capital | +---------+------------+ | 1 | Baku | | 2 | Tokyo | | 3 | Berlin | | 4 | Washington | | 6 | Ankara | +---------+------------+
Now we need show capitals and country names according to country code. You can do it with using JOIN or VIEW etc. And also you can simply use subquery like this:
mysql> SELECT capital,(SELECT name FROM countries WHERE code=country) AS country FROM capitals; +------------+------------+ | capital | country | +------------+------------+ | Baku | Azerbaijan | | Tokyo | Japan | | Berlin | Germany | | Washington | USA | | Ankara | Turkey | +------------+------------+
This method is not too good for performance point-view, but, you can use it in some situations.
Reference:
http://dev.mysql.com/doc/refman/5.0/en/subqueries.html
