Reputation: 4480
I'm using PHP and MySQL. Is this the best method to select information about "user_1" from four different tables? I know it works, because I have tried it. But is this the preferred method of selecting information from multiple tables for "user_1"?
$query = "SELECT table_1.username, table_2.city, table_3.state, table_4.country
FROM table_1
JOIN table_2
ON table_1.username=table_2.username
JOIN table_3
ON table_1.username=table_3.username
JOIN table_4
ON table_1.username=table_4.username
WHERE table_1.username = 'user_1'";
Upvotes: 1
Views: 314
Reputation: 1190
I don't think your example will work, at least the way you have described it. You are selecting from table_2, where the "city" field equals the "username" field. You might mean this:
ON table_1.city = table_2.city
And so on the for the rest of them. But yes, in general, it is acceptable to use JOINs with relational databases, although in this specific example, I am not sure what you are trying to do exactly.
Upvotes: 4