0

我想从三个表中得到一个结果。我不知道如何编写 sql 查询。请帮我。

"I want to display Name,Username and Product_Name where Id=007"

table "register"

Name  Username  Id
Arj   arjun     007
xyz   abcd      008
abcd  asdf      007

table "products"

Product_Id   Product_Name   Price
101           Clothes        200
102           Games          100

table "purchase"     //products.Product_Id=purchase.Item

Username   item    Id
arjun      102     007
abcd       101     008
asdf       102     007
4

3 回答 3

0

应该是这样的

SELECT * FROM
register r
JOIN purchase p on p.username = r.username
JOIN products pr on pr.product_id = p.item

您可以修改此基本代码以包含特定列并添加 where 和 order 子句。

于 2013-02-05T16:43:25.080 回答
0

试试这个查询:

SELECT a.Name,a.Username,c.Product_Name
FROM  register as a
JOIN purchase as b on a.Username=b.Username
JOIN products as c on b.item=c.Product_Id

它应该像这样工作。要阅读有关 SQL 连接的更多信息,请尝试:http ://www.w3schools.com/sql/sql_join.asp

享受!

于 2013-02-05T16:39:41.350 回答
0
SELECT Username, Id, Product_Name 
FROM register 
INNER JOIN purchase ON purchase.Id = register.Id 
INNER JOIN products ON purchase.Item = products.Product_Id
WHERE Id = '007'

附带说明:您也不应该将用户名保存在购买表中。

于 2013-02-05T16:40:11.830 回答