MySQL sql如何从两个表中获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9806097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:39:48  来源:igfitidea点击:

How to fetch data from two tables in sql

mysqlsql

提问by ak274

I have two tables:

我有两个表:

This is table1:

这是表1:

product_id|product_desc|product_name|product_s_desc

This is table2:

这是表2:

product_price_id|product_id|product_price

Now I want to fetch data from these tables. product_idis same in both tables.

现在我想从这些表中获取数据。product_id在两个表中是相同的。

I want to fetch

我想取

  1. product_s_desc
  2. product_desc
  3. product_nameAND product_pricefrom other table.
  1. product_s_desc
  2. product_desc
  3. product_nameANDproduct_price来自其他表。

Please help me do this.

请帮我做这件事。

回答by Crontab

I'm assuming you have a field named product_pricein your second table (you didn't list it):

我假设您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

You should check out the MySQL manual regarding JOINS, as this is a very basic part of writing SQL queries. You might also consider adding an index on table2for the product_id field to make the query run faster.

您应该查看有关 的 MySQL 手册JOINS,因为这是编写 SQL 查询的一个非常基本的部分。您还可以考虑table2为 product_id 字段添加一个索引,以使查询运行得更快。

回答by SmartestVEGA

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

回答by user1127214

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

回答by sachin patil

$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;";