开发者

Selecting info from multiple tables for one unique ID with PHP and MySQL

开发者 https://www.devze.com 2022-12-24 04:32 出处:网络
I\'m using PHP and MySQL. Is this the best method to select information about \"user_1\" from four different t开发者_运维百科ables?I know it works, because I have tried it. But is this the preferred m

I'm using PHP and MySQL. Is this the best method to select information about "user_1" from four different t开发者_运维百科ables? 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'";


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号