0

Possible Duplicate:
How can an SQL query return data from multiple tables

Please help me to resolve this, I have 2 mysql tables,

tbl_product

id  name         title  userID
1   Phone        N95       1
2   Tab          Google    1
3   Laptop       Toshiba   1
4   PhoneNext    Nokia     2
5   Mp3 Player   Apple     2
6   Gallexy      Samsung   3
7   Hard         320GB     6

tbl_user

id selName
1  Jhon
2  Khan
3  Mohomad
4  Ann
6  Ricky

I need to get Like this output joining with these two tables,

Phone N95 (userID = 1)

Tab Google (userID = 1)

Laptop Toshiba (userID = 1)

Jhon (id=1)

PhoneNext Nokia (userID = 2)

Mp3 Player Apple (userID = 2)

Khan (id=2)

Gallexy Samsung (userID = 3)

Mohomad (id=3)

Please help me guys Thanks all....

I Need to get Like This out put,
<table width="224" border="1"> <tr> <td width="214">Phone N95 (userID = 1)</td> </tr> <tr> <td>Tab Google (userID = 1)</td> </tr> <tr> <td>Laptop Toshiba (userID = 1)</td> </tr> <tr> <td align="right" bgcolor="#FFFF99">Jhon (id=1)</td> </tr> <tr> <td>PhoneNext Nokia (userID = 2)</td> </tr> <tr> <td>Mp3 Player Apple (userID = 2)</td> </tr> <tr> <td align="right" bgcolor="#FFFF99">Khan (id=2)</td> </tr> </table> 
Community
  • 1
  • 1
Thush
  • 1
  • 4
  • 2
    What have you tried? See [how can an SQL query return data from multiple tables](http://stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables). – DCoder Oct 27 '12 at 07:26

2 Answers2

0

Can't you use something like this?

Select title,userID from tbl_product 
where tbl_product.userID = tbl_user.id 
group by userID
Delphinium
  • 115
  • 1
  • 2
  • 10
  • 1
    No i cant't display User Name Div correclty inside while loop – Thush Oct 27 '12 at 07:45
  • I can't get tha output using PHP, NameID field show every item name beleow. But I need to get That NameID after Group duplicates, – Thush Oct 27 '12 at 07:59
  • you can store your sql query in a variable and then take out the appropriate attribute out of the variable something like this: $ch=mysql_query(//your query);$num_rows = mysql_num_rows($id3);while($row = mysql_fetch_array($ch)){ array_push($title, $row['Title']);} and so on... – Delphinium Oct 27 '12 at 15:51
  • sorry it should be $ch not $id3; also $title is the array in which you will be "pushing" your output array – Delphinium Oct 27 '12 at 15:58
0
SELECT tbl_product.userID, tbl_product.name, tbl_product.title, tbl_user.selName 
FROM tbl_product
LEFT JOIN tbl_user ON tbl_product.userID = tbl_user.id
GROUP BY tbl_product.userID

I recommend to start reading the basics about MySQL.

MySQL DOC

MySQL SELECT

MySQL JOIN

enenen
  • 1,967
  • 2
  • 17
  • 33
  • Dear SQL is correct but I can't get out put what I have mentioned above, When I call with this Query using PHP , UserID field show any below item Divs – Thush Oct 27 '12 at 07:57
  • Edited. It would be better for you to start with the MySQL documentation, I think. – enenen Oct 27 '12 at 07:59
  • Hey I no need to start to SQL basic , This is what I need to display Using PHP MYSQL Thanks!
    Phone N95 (userID = 1)
    Tab Google (userID = 1)
    Laptop Toshiba (userID = 1)
    Jhon (id=1)
    PhoneNext Nokia (userID = 2)
    Mp3 Player Apple (userID = 2)
    Khan (id=2)
    – Thush Oct 27 '12 at 08:20
  • You have to execute your query and just iterate through the returned data and create the table. So, what is the problem? – enenen Oct 27 '12 at 08:46