MySQL 特定列的内部联接

MySQL inner join for specific column

我有 2 个 table:

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

我正在尝试在具有内部连接的页面中显示产品图片 table,但问题是我对 added_on 和 modified_on 的名称相同,我想 [=42] =] 来自 table product product_id, product_name, price 和来自 table product_image all.

到目前为止我已经使用了这个查询:

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

商品图片展示:

只需使用 table 名称作为别名:

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

通过这种方式,您可以确保从您想要的 table 中获取列。

不要 select 所有的列。使用 tableName.columnName

从任何 table 中需要的 select 列
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