sql如何从两个表中获取数据 [英] How to fetch data from two tables in sql
本文介绍了sql如何从两个表中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两张桌子:
这是表 1:
product_id|product_desc|product_name|product_s_desc
这是表 2:
product_price_id|product_id|product_price
现在我想从这些表中获取数据.product_id
在两个表中相同.
Now I want to fetch data from these tables. product_id
is same in both tables.
我要取货
product_s_desc
product_desc
product_name
ANDproduct_price
来自其他表.
product_s_desc
product_desc
product_name
ANDproduct_price
from other table.
请帮我做这件事.
推荐答案
我假设您的第二个表中有一个名为 product_price
的字段(您没有列出):
I'm assuming you have a field named product_price
in your second table (you didn't list it):
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
您应该查看有关 JOINS的 MySQL 手册code>
,因为这是编写 SQL 查询的一个非常基本的部分.您还可以考虑在 table2
上为 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 table2
for the product_id field to make the query run faster.
这篇关于sql如何从两个表中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文