ak274
ak274

Reputation: 87

How to fetch data from two tables in sql

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.

Upvotes: 3

Views: 36182

Answers (4)

user1127214
user1127214

Reputation: 3177

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

Upvotes: 1

sachin patil
sachin patil

Reputation: 1

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

Upvotes: -4

SmartestVEGA
SmartestVEGA

Reputation: 8889

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

Upvotes: 1

WWW
WWW

Reputation: 9860

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.

Upvotes: 6

Related Questions