[英]mysql select from where a condition is met
My table structure is as follows 我的表结构如下
ProductID | Image | Default
1 1.jpg N
1 2.jpg Y
1 3.jpg N
2 4.jpg N
2 5.jpg N
3 6.jpg Y
3 7.jpg N
I'm Trying to get a return of all producID where there is no default image 'N' , eg the above should return productID 2. Sorry no code attempt , as i'm not sure what to do at all. 我正在尝试返回没有默认图像'N'的所有producID,例如,上面的代码应该返回productID2。抱歉,没有代码尝试,因为我不确定该怎么做。
Group by the product and select only those having none default=Y
按产品分组并仅选择没有default=Y
select productID
from your_table
group by productID
having sum(`default` = 'Y') = 0
Try this: 尝试这个:
SELECT t1.productID
FROM yourtable t1
WHERE NOT EXISTS(
SELECT 'DEFAULT'
FROM yourtable t2
WHERE T2.productid = t1.productid
AND T2.`default` = 'Y'
)
This SQL code does what you want:
SELECT productID FROM
(SELECT productID, SUM(table.default = 'Y') AS Y_count
FROM table
GROUP BY productID) AS tbl
WHERE Y_count = 0
如果default是数据库中的一列,则可以使用:
Select * from mytabale where 'Default'='Y'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.