I came across a good way to write queries in mysql. Instead of using INNER JOIN there is a easy way to do it, sort of renaming the tables while writing query. Below is the example:
Requires: MySQL
Using:
Explanation:
Look at the query renaming of table is done before retrieving the information we want. WHERE clause is very important in the query because there should be something which is common in both the tables in order to fetch the data.
Note: You can also write queries instead of table name in FROM clause i.e., sub-queries.
If you are not getting results as you expect, there is something wrong with the way query has been written. Try writing it some other way. And this will happen a lot with complex queries. :P
So, this method will make your queries really powerful and easy. Time to replace INNER JOIN.
Requires: MySQL
Using:
table1( id int, name varchar(30) age int) table2( id int, address varchar(300)) SELECT a.name, a.age, b.address FROM table1 a, table2 b WHERE a.id = b.id;
Explanation:
Look at the query renaming of table is done before retrieving the information we want. WHERE clause is very important in the query because there should be something which is common in both the tables in order to fetch the data.
Note: You can also write queries instead of table name in FROM clause i.e., sub-queries.
If you are not getting results as you expect, there is something wrong with the way query has been written. Try writing it some other way. And this will happen a lot with complex queries. :P
So, this method will make your queries really powerful and easy. Time to replace INNER JOIN.
No comments:
Post a Comment
Thank you for your comment!