I am using php with Mysql.
I want to know what really is wrong with query inside a query, what if i am not that much comfortable with using the INNER JOIN , and rather prefer writing multiple queries.
Am i doing something terribly wrong, or is it just to make it visually sound?
What I mean is…
$query="SELECT * FROM TABLE WHERE USER_ID=1";
$data=mysqli_query($dbc,$query);
$row=mysqli_fetch_array($data);
$query2="select * from TAble2 where some_id='{$row['user_id']}'";
$data1=mysqli_query($dbc,$query);
$row1=mysqli_fetch_array($data1);
// and then use $row1['column_name']
The main benefit to combining as much as possible into a single query as opposed to iterating nested cursors is that round trips to the DB are relatively expensive.
Using the nested cursors method in your question, you are preparing and executing n + 1 SQL statements, where n is the number of records in the initial result set.
Where you to combine those queries with a join, you would only be preparing and executing one statement, no matter how large the result set.
You’re also placing less load on the database itself, particularly where statement caching is enabled.