1

I will try to be short. I got two tables:

Users
ID | Name
1  | Joe
2  | Jimmy

Phone
ID | Number | User
1  | 12345  |  1
2  | 56789  |  2

Now I want generate a query to display all phones, but instead of showing the user number I want show his name.

Would be something like -> query * from phone ... while row ... echo $row->id, $row->number, $row->User+Name.

How can I do that? I can accomplish doing 2 queries, first 1 saving to an array and the second one modifying the elements of that array, but probably mysql got a better and faster solution for this.

Thanks

Marc B
  • 356,200
  • 43
  • 426
  • 500
Henrique
  • 21
  • 3

2 Answers2

1
SELECT Phone.ID, Phone.Number, Users.Name
FROM Phone
LEFT JOIN Users ON Phone.User = Users.ID
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

select u.name, p.number from users u, phone p where u.id = p.user

unless phone doesn't know have an FK to user then use the outer join but only if there is a phone without a user

SWD
  • 289
  • 1
  • 5
  • 13