0

可能重复:
SQL 查询如何从多个表中返回数据

请帮我解决这个问题,我有 2 个 mysql 表,

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

我需要得到这样的输出加入这两个表,

Phone N95 (userID = 1)

Tab Google (userID = 1)

Laptop Toshiba (userID = 1)

约翰 (id=1)

PhoneNext Nokia (userID = 2)

Mp3 Player Apple (userID = 2)

汗 (id=2)

Gallexy Samsung (userID = 3)

穆罕默德 (id=3)

请大家帮帮我谢谢大家......

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> 
4

2 回答 2

0

你不能用这样的东西吗?

Select title,userID from tbl_product 
where tbl_product.userID = tbl_user.id 
group by userID
于 2012-10-27T07:34:08.293 回答
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

我建议开始阅读有关 MySQL 的基础知识。

MySQL 文档

MySQL 选择

MySQL 加入

于 2012-10-27T07:52:20.173 回答