Mysql 仅选择其他表中行数大于0的行

Mysql 仅选择其他表中行数大于0的行,mysql,Mysql,我的数据库中有两个表: Products: -------------------------------------------------- | id | product_name | manufacturer | -------------------------------------------------- Products_photos: ----------------------------------------------- | id

我的数据库中有两个表:

Products:
--------------------------------------------------
|   id    |   product_name    |   manufacturer   |
--------------------------------------------------

Products_photos:
-----------------------------------------------
|   id    |   product_id    |   image_name    |
-----------------------------------------------
我想选择所有产品,其中产品照片计数大于0。 我怎么能做到

@编辑: 我不想为我的输出添加产品和照片的结果。我只想显示来自产品的条目,其中有任何图像。对不起,我的英语:)

谢谢你的帮助

SELECT P.* FROM Products AS P INNER JOIN Products_Photos AS PP ON P.id=PP.id
另一种方法,效率更低,但可能更好地让你理解,是

SELECT P.* FROM Products AS P
WHERE P.id IN (SELECT DISTINCT id FROM Product_photos)
你能行

Select P.id, P.product_name, P.manufacturer
from Products P 
INNER JOIN Products_photos Pp on P.Id = Pp.product_id

对于内部联接,它将只返回可能进行联接的行,这意味着您在
Products\u photos
表中至少有一个值。

就查询效率而言,我认为已经提供的联接解决方案是最好的选择。但为了清楚起见——就准确表达你的要求而言——我会选择这样一种方法:

select * from products p
where exists (select * from products_photos pp where pp.product_id = p.id)

你能解释一下这是怎么做到的吗:“来自产品-->p@PiKey,AS是可选的。有些人喜欢它的可读性,其他人则认为它是混乱的。这两种解决方案之间没有区别。所以它是这样工作的:)Thx。你的例子对我来说是错误的,因为我不想为结果添加照片。我只想显示产品,其中有任何照片。内部连接不会将照片信息添加到输出中。仅显示产品字段(第页*)。内部联接仅包括在两个表中显示的产品,但结果输出仅用于产品表。
select * from products p
where exists (select * from products_photos pp where pp.product_id = p.id)