0

Is it possible to combine the following 3 MySQL tables using PHP?

  • Table 1: Columns - userid, username, userpass
  • Table 2: Columns - accountid, accounttype
  • Table 3: Columns - userid, accountid, date, rating

Please note that I am new to mySQL and PHP

Raidri
  • 17,258
  • 9
  • 62
  • 65
E3563
  • 1
  • 1
  • 2
    you need a simple JOIN in sql – GorillaApe Oct 06 '13 at 19:22
  • I have tried several join functions, but from videos I am starting to think that that is only in myphpadmin. I am asking if it is possible to join the tables with only php script. – E3563 Oct 06 '13 at 19:50
  • One example of php that I tried: $join ="SELECT table1.userid, table2.accountid FROM table3 INNER JOIN databasename ON table1.userid=table3.userid"; – E3563 Oct 06 '13 at 19:53

2 Answers2

1

To fetch, for example, a result-set of usernames with their account types and ratings you'd use:

SELECT table1.username, table2.accounttype, table3.rating FROM table1
JOIN table3 ON table3.userid = table1.userid
JOIN table2 ON table2.accountid = table3.accountid;

In the future, I recommend listing your tables with descriptive names, and in the order by which they will be joined to acheive the result-set you need.

Hope this helps

Brandon M.
  • 278
  • 1
  • 4
  • 15
0

Use left join for better result. first table left join second table (condition) left join third table (condition)