Orange Fox
Orange Fox

Reputation: 147

Mysql: get result from 2 tables

I have 2 tables.

Phonebook:

 _________________________________
| id | Photo   | Name | Number    |
|---------------------------------|
| 1  | abc.jpg | John | 123-45-67 |
|---------------------------------|
| 2  | def.jpg | Sam  | 482-34-00 |
|_________________________________|

History:

 ____________________________________
| id  | Name | Date       | Type     |
|------------------------------------|
| 24  | John | 17.03.2014 | Incoming |
|------------------------------------|
| 25  | Sam  | 18.03.2014 | Incoming |
|------------------------------------|
| 26  | Sam  | 19.03.2014 | Outgoing |
|____________________________________|

I need to get all columns from History table where id = $id (25 in my case) and get Image column from Phoneboock where History.Name = Phonebook.Name (Sam in my case). Below is the result that I want to get:

 ______________________________________________
| id  | Name | Date       | Type     | Image   |
|----------------------------------------------|
| 25  | Sam  | 18.03.2014 | Incoming | def.jpg |
|______________________________________________|

Upvotes: 0

Views: 46

Answers (3)

Julie Beck
Julie Beck

Reputation: 258

If you are looking for the SQL Select statement, it would look something like this:

SELECT HISTORY.*, PHONEBOOK.Photo FROM HISTORY, PHONEBOOK
WHERE HISTORY.Name = PHONEBOOK.Name AND HISTORY.id='25'

Upvotes: 1

Arijoon
Arijoon

Reputation: 2300

You can use JOIN as follows:

SELECT * FROM History INNER JOIN Phonebook WHERE  History.name = Phonebook.name

That will give you a join of all the rows. You can customize it more to get what you want. Look at JOIN here for more info

Upvotes: 1

Sal00m
Sal00m

Reputation: 2916

It seems your problem is the SQL to get your data, so use this:

SELECT History.*, Phonebook.Photo
FROM History INNER JOIN Phonebook ON Phonebook.Name = History.Name 
AND History.id = $id

Upvotes: 2

Related Questions