(以下架构已缩小以减少问题)
我有两张桌子Products
:
| ID | ProductName | AvailableDate (date)
|------------------------------------------------
| 1 | Foo | 2011-01-01
| 2 | Bar | 2017-01-01
| 3 | FooBar | 2011-01-01
和Mappings
:
| ID | ProductID | SomeOtherID
|------------------------------------------------
| 1 | 1 | 1
| 2 | 2 | 1
| 3 | 3 | 2
映射可以指向n
产品:
SELECT
GROUP_CONCAT(`Products`.ProductName SEPARATOR ', ')
FROM
Mappings
JOIN
Products ON Mappings.ProductID = Products.ID
GROUP BY
Mappings.SomeOtherID
我现在想要扩展查询以检查 Products
中的任何行是否具有大于当前日期的AvailableDate来发现组合的产品,这些产品至少具有一个“早期访问”产品。
我尝试了以下
SELECT
GROUP_CONCAT(`Products`.ProductName SEPARATOR ', '),
IF(COUNT(DATEDIFF(`Products`.AvailableDate, CURDATE)) > 0, 1, 0) As IsEarlyAccess
FROM
Mappings
JOIN
Products ON Mappings.ProductID = Products.ID
GROUP BY
Mappings.SomeOtherID
但我总是收到IsEarlyAccess = 1
。任何提示?
答案 0 :(得分:1)
我很想在MAX上使用IF而不是COUNTing IF: -
SELECT GROUP_CONCAT(Products.ProductName SEPARATOR ', '), IF(MAX(Products.AvailableDate) > CURDATE(), 1, 0) As IsEarlyAccess
FROM Mappings
JOIN Products ON Mappings.ProductID = Products.ID
GROUP BY Mappings.SomeOtherID
答案 1 :(得分:1)
你的问题在这里:
IF(COUNT(DATEDIFF(`Products`.AvailableDate, CURDATE)) > 0, 1, 0) As IsEarlyAccess
你的计数是这样的:
COUNT(DATEDIFF(`Products`.AvailableDate, CURDATE))
对于任何记录,无论结果如何,都将计为1,因为它将以count(4)
count(-2)
ETC执行..
将其更改为:
IF(SUM(DATEDIFF(`Products`.AvailableDate, CURDATE) > 0) > 0, 1 , 0)
对于每个记录,其可用日期大于今天的记录将为1。
答案 2 :(得分:1)
在MySQL中你实际上可以逃脱:
CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
GRANT SELECT, INSERT, DELETE, UPDATE, CREATE, ALTER, EXECUTE ON schema.* TO 'user'@'localhost';
FLUSH PRIVILEGES;
或
this.get('activation_codes/:id', function(db){
return {
data: {
type: 'activation-code',
id: db.activation-codes[0].id,
attributes: db.activation-codes[0]
}
};
});
MySQL将布尔值设为1,将false设为0。