Valor_
Valor_

Reputation: 3591

Correct join method for mysql tables in current case

I'm trying to figure it out what is the correct join method for my case of joining tables. I have following tables

I have a table users

id_user   name
  1       Mark
  2       Tesla
  3       Nick

and i have table orders

id_order  id_user
  1         1
  2         2
  3         1
  4         1

My problem is that i have to select user and join table orders, so when i query one user i also have to see all of his orders. My current code selects only one order of particular user

SELECT * FROM users AS u
JOIN orders AS o
ON o.id_user = u.id_user
WHERE u.id_user = 1

I'm doing this completely wrong way? By the way i will use this code with PHP, so i can provide a object of user with all orders, when getting one user, if that information matters at all. If you need any additional information's please let me know and i will provide. Thank you in advance.

Upvotes: 0

Views: 40

Answers (1)

Ahsan Arshad
Ahsan Arshad

Reputation: 116

I guess you are looking for this..

SELECT orders.id_order, users.name FROM orders
INNER JOIN users
ON orders.id_user=users.id_user 
WHERE users.id_user = 1;

Upvotes: 1

Related Questions