我的MySQL查询是正常还是可以改进?

时间:2016-05-17 13:30:53

标签: mysql

我写了一个查询来从MySQL中选择产品:

这就是它的样子:

SELECT `products`.`id` AS `id`, `products`.`name` AS `name`, `products`.`slug` AS `slug`, `products`.`ord` AS `ord`, 
SUBSTRING_INDEX(GROUP_CONCAT(products_photos.name ORDER BY products_photos.ord ASC SEPARATOR ","), ",", 1) AS `photo`, 
SUBSTRING_INDEX(GROUP_CONCAT(products_attributes.price ORDER BY products_attributes.price ASC SEPARATOR ","), ",", 1) AS `price` 

FROM `products` 

LEFT JOIN `products_photos` ON `products`.`id` = `products_photos`.`product_id` 
LEFT JOIN `products_attributes` ON `products`.`id` = `products_attributes`.`product_id` 
LEFT JOIN `products_categories` ON `products`.`id` = `products_categories`.`product_id` 

WHERE `products_categories`.`category_id` = '5' AND `products`.`status` = '1' 

GROUP BY `products`.`id` 

ORDER BY `ord` ASC

我想知道查询的照片和属性部分,是否有更好的方法从连接表中选择第一张照片和最小价格? 我的桌子上会有很多产品,我担心未来的表现,因为我不擅长MySQL:)

任何指针都会受到赞赏:)

0 个答案:

没有答案