I'm at the point where i need to learn to use more advanced features of SQL if i would like to advance my career. I confess i don't know what a join is or how to use them. i start reading queries with joins and my brain just turns to mush, but i have encountered a situation where i might benefit from using a join, but i don't know if what i want to do is even possible.
consider the following tables:
user
id | user_name | location_id
===============================
1 | r3wt | 316
location
id | state | city
=========================
316 | Clarksville | Arkansas
Now when i select the user, i get back an array like
[
id => 1,
user_name => r3wt ,
location_id =>316
]
What i would like to get back is:
[
id => 1,
user_name => r3wt ,
location_id =>[
id=>316,
city=>Clarksville,
state=>Arkansas
]
]
I'm wondering if this is possible, and if so how i might alter my quite bland select query to make this possible. Thanks, and if you need more details or want me to make what will be a pathetic attempt at figuring it out myself, i am willing to embarrass myself to learn. thank you
My pathetic little attempt which of course doesnt work:
SELECT id, user_name, location AS location_id
FROM user JOIN location ON location.id = user.location_id
Keep in mind i have absolutely no idea what i'm doing. i don't even understand what joins are and how they work, but i understand what i would like to be able to do with SQL.
You should:
This will give you almost what you wanted but as a single flat array:-
SQL results are always a two dimensional table. This is fundamental to the way SQL works! The "LEFT OUTER" means you want null values for city and state in the result if no location is present.
Just google "MYSQL TUTORIAL" and you will find dozens of sites offering on-line guides for free. Cannot recommend a particular one as I learnt all this stuff in pre-history.