首页 > 解决方案 > 特定列的 MySQL 内部联接

问题描述

我有 2 张桌子:

product

product_id 产品名称 价格 添加了_on modified_on
1 电话 100 2021-09-25 2021-09-25

product_image

ID product_id product_image1 添加了_on modified_on
1 1 手机图片.jpg 2021-09-27 2021-09-27

我正在尝试在具有内部联接的页面中显示产品图像表,但问题是我的 added_on 和 modified_on 具有相同的名称,我想从 table product product_idproduct_namepricetable product_imageall 中进行选择。

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

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

产品图片展示:

在此处输入图像描述

标签: mysqlsqlinner-join

解决方案


不要选择所有列。使用从任何表中选择您需要的列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 

推荐阅读