如何从两个表中获取SQL数据

3

我有两个表格:

这是表格1:

product_id|product_desc|product_name|product_s_desc

这是table2:

product_price_id|product_id|product_price

现在我想从这些表中获取数据。两个表中的product_id是相同的。
我想获取:
  1. product_s_desc
  2. product_desc
  3. 来自另一个表的product_nameproduct_price
请帮我完成这个任务。

抱歉!table2也有product_price字段。 - ak274
4个回答

6
我假设你的第二个表中有一个名为product_price的字段(你没有列出它):
SELECT t1.product_s_desc, t1.product_desc, t1.product_name, t2.product_price
FROM table1 t1
INNER JOIN table2 t2 ON t2.product_id = t1.product_id

你应该查看MySQL手册中关于JOINS的内容,因为这是编写SQL查询的非常基础的部分。你还可以考虑在table2的product_id字段上添加索引,以使查询运行更快。

1
Select * from table1 join table2 on table1.productid = table2.productid

1
SELECT t1.*,t2.product_price  
FROM table1 t1,table2 t2 
WHERE t1.product_id=t2.product_id 

-4
$sql = "SELECT Student.First_Name,Student.Last_name,Student.Mobile_No,Student.Email,Student.Institue,Student.DOB,Student.Gender
            Address.Address_Line1,Address.City,Address.State,Address.Country,Address.Zip_code
        FROM   Student INNER JOIN Address
        ON     Student.Id=Address.Id;";  

你是不是想在这个问题下发表评论?这个问题是关于产品的,而你发表的是关于学生的。此外,这个问题已经在4年前提出并得到回答了。 - David Makogon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接