MySQL inner join for specific column

Viewed 457

I have 2 tables:

table product:

product_id product_name price added_on modified_on
1 Phone 100 2021-09-25 2021-09-25

table product_image:

id product_id product_image1 added_on modified_on
1 1 phoneimage.jpg 2021-09-27 2021-09-27

I am trying to display product image table in a page with inner join but the problem is that I have same name for added_on and modified_on I want to select from table product product_id, product_name, price and from table product_image all.

So far I have used this query:

SELECT * FROM `product_image` 
INNER JOIN `product` ON product_image.product_id = product.product_id 
ORDER BY `id` DESC   

product image display:

enter image description here

2 Answers

Don't select all the columns. select columns which you need from any table using tableName.columnName

SELECT 
product.product_id,
product.product_name,
product.price ,
product_image.added_on,
product_image.modified_on 
FROM product_image 
inner join product 
on product_image.product_id = product.product_id 
ORDER BY id DESC 

Just use it with the table name as alias:

SELECT product.product_id,
    product.product_name,
    product.price,
    product_image.id,
    product_image.product_id,
    product_image.product_image1,
    product_image.added_on,
    product_image.modified_on
FROM product_image
INNER JOIN product ON product_image.product_id = product.product_id
ORDER BY product_image.id DESC

This way you make sure you take the columns from the table you want to.

Related