Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch data from two tables in sql

Tags:

sql

mysql

I have two tables:

This is table1:

product_id|product_desc|product_name|product_s_desc

This is table2:

product_price_id|product_id|product_price

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

I want to fetch

  1. product_s_desc
  2. product_desc
  3. product_name AND product_price from other table.

Please help me do this.

like image 771
ak274 Avatar asked Nov 19 '25 10:11

ak274


2 Answers

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

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.

like image 138
WWW Avatar answered Nov 21 '25 22:11

WWW


Select * from table1 join table2 on table1.productid = table2.productid
like image 24
SmartestVEGA Avatar answered Nov 22 '25 00:11

SmartestVEGA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!