-6

我按 ID 选择照片,按订单字段选择下一张照片。

我检查照片 id = 33 的存在并按顺序查找下一张照片

SELECT `id` FROM `product_photo` 
WHERE 
`product_id` = (SELECT `product_id` FROM `product_photo` WHERE `id` = '33' ) 
AND 
`order` >= (SELECT `order` FROM `product_photo` WHERE `id` = '33' ) 
LIMIT 2

是否可以简化此查询?

4

1 回答 1

2

听说过JOINing 表吗?

SELECT photo.id
FROM product_photo
INNER JOIN product_photo AS photo ON photo.product_id = product_photo.product_id
   AND photo.order >= product_photo.order
WHERE product_photo.id = '33';

FROMand部分将WHERE选择您的产品id = 33,然后您product_photo使用不同的别名加入(是的,您可以在一个查询中多次加入同一个表)product_idorder

于 2013-09-20T06:47:46.450 回答