我一直在努力让以下问题得以运行:
SELECT user.username, user.email, user.id, user.last_mail, us.24hr
FROM wine_users user, wine_subscriptions sub, wine_user_settings us
WHERE user.id = sub.user_id
AND us.id = user.id
AND sub.wine1 = 'yes'
AND user.receive_mail = 'yes'
AND if(24hr = 'yes') user.last_mail >= DATE_SUB (user.last_mail INTERVAL 1 DAY)
基本上,我有3个表,包含用户名(wine_users
),订阅(wine_subscriptions
)和用户设置(user_settings
)
我想要的是选择在我们中选择了NO的用户.24小时或在我们中选择YES.24hr(在我们中存储为ENUM'yes''no'。24小时)并且没有收到e-最近24小时内的邮件(这是作为TIMESTAMP存储在user.lastmail中)
任何帮助都会阻止我通过我的左鼻孔撕掉我自己的大脑! :)
谢谢:)
的Seb
哇!你们真棒! :)尝试了所有的建议,但他们都返回以下错误:
1064 - 您的SQL语法出错;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 在'INTERVAL 1 DAY)附近))限制0,30'在第8行
我猜这与DATE_SUB命令有关,不喜欢user.last_mail字段的TIMESTAMP格式......这可能是正确的,如果是这样,我该如何处理呢?
为了清楚起见,脚本是一个页面的一部分,每隔一小时就会被chron点燃,我已经为我做过了。几个朋友。它检查特易购葡萄酒是否有“神秘葡萄酒包”(因为网上设计总是更好用廉价葡萄酒!)
如果有的话(每周只发生一次或所以它通过电子邮件发送用户数据库中的任何人,他们将24小时设置为否(即产品可用时每小时发送一次电子邮件)或者24小时设置为YES并且没有收到电子邮件。最后24小时
(因此last_mail字段,记录发送最后一封电子邮件时的TIMESTAMP)
非常感谢回复!
Seb:)
答案 0 :(得分:0)
看看这是否有效。
SELECT user.username, user.email, user.id, user.last_mail, us.24hr
FROM wine_users user, wine_subscriptions sub, wine_user_settings us
WHERE user.id = sub.user_id
AND us.id = user.id
AND sub.wine1 = 'yes'
AND user.receive_mail = 'yes'
AND ( (24hr = 'yes' AND user.last_mail >= DATE_SUB (user.last_mail INTERVAL 1 DAY)) OR 24hr='no')
答案 1 :(得分:0)
我从你的问题中假设必须满足以下两个条件中的一个:
一个。在我们中选择NO的用户.24hr
B中。在我们中选择YES的用户.24小时并且在过去24小时内没有收到电子邮件
SELECT user.username, user.email, user.id, user.last_mail, us.24hr
FROM wine_users user, wine_subscriptions sub, wine_user_settings us
WHERE user.id = sub.user_id
AND us.id = user.id
AND sub.wine1 = 'yes'
AND (
user.receive_mail = 'no'
OR (user.24hr = 'yes'
AND DATE_SUB(user.last_mail interval 1 day) > user.last_Mail)
)
答案 2 :(得分:0)
如果我没有读错你的问题,你可以使用联盟:
SELECT user.username, user.email, user.id, user.last_mail, us.24hr
FROM wine_users user, wine_subscriptions sub, wine_user_settings us
WHERE user.id = sub.user_id
AND us.id = user.id
AND sub.wine1 = 'yes'
AND user.receive_mail = 'yes'
AND if(24hr = 'yes') user.last_mail >= DATE_SUB (user.last_mail INTERVAL 1 DAY)
UNION ALL
SELECT user.username, user.email, user.id, user.last_mail, us.24hr
FROM wine_users user, wine_subscriptions sub, wine_user_settings us
WHERE user.id = sub.user_id
AND us.id = user.id
AND sub.wine1 = 'yes'
AND user.receive_mail = 'yes'
AND 24hr = 'No'
答案 3 :(得分:0)
我想你可能正在寻找这个:
SELECT user.username, user.email, user.id, user.last_mail, us.24hr
FROM wine_users user, wine_subscriptions sub, wine_user_settings us
WHERE user.id = sub.user_id
AND us.id = user.id
AND sub.wine1 = 'yes'
AND user.receive_mail = 'yes'
AND (
(24hr = 'yes' AND user.last_mail >= DATE_SUB (user.last_mail, INTERVAL 1 DAY))
OR 24hr = 'no'
)
但我建议您使用JOIN
进行重新设置,例如:
SELECT user.username, user.email, user.id, user.last_mail, us.24hr
FROM wine_users AS user
JOIN (wine_user_settings AS us, wine_subscriptions AS sub)
ON (us.id = user.id AND us.id = sub.user_id)
WHERE sub.wine1 = 'yes'
AND user.receive_mail = 'yes'
AND (
(24hr = 'yes' AND user.last_mail >= DATE_SUB (user.last_mail, INTERVAL 1 DAY))
OR 24hr = 'no'
)
不是我可以测试它。
答案 4 :(得分:0)
您可以使用OR
:
SELECT user.username, user.email, user.id, user.last_mail, us.24hr
FROM wine_users user, wine_subscriptions sub, wine_user_settings us
WHERE user.id = sub.user_id
AND us.id = user.id
AND sub.wine1 = 'yes'
AND user.receive_mail = 'yes'
AND (24hr = 'no' OR
(24hr = 'yes' AND user.last_mail < DATE_SUB(NOW(), INTERVAL 1 DAY)))
您可以使用UNION ALL
:
SELECT user.username, user.email, user.id, user.last_mail, us.24hr
FROM wine_users user, wine_subscriptions sub, wine_user_settings us
WHERE user.id = sub.user_id
AND us.id = user.id
AND sub.wine1 = 'yes'
AND user.receive_mail = 'yes'
AND 24hr = 'yes'
AND user.last_mail < DATE_SUB(NOW(), INTERVAL 1 DAY)
UNION ALL
SELECT user.username, user.email, user.id, user.last_mail, us.24hr
FROM wine_users user, wine_subscriptions sub, wine_user_settings us
WHERE user.id = sub.user_id
AND us.id = user.id
AND sub.wine1 = 'yes'
AND user.receive_mail = 'yes'
AND 24hr = 'no'
您有时会发现UNION ALL
更有效率,即使它打字更多,因为数据库引擎可以找到更优化的执行计划。
答案 5 :(得分:0)
user.last_mail >= DATE_SUB (user.last_mail INTERVAL 1 DAY)
永远不会是False
!
我认为你需要使用函数NOW()
和一些括号OR
:
SELECT user.username
, user.email
, user.id
, user.last_mail
, us.24hr
FROM wine_users AS user
JOIN wine_subscriptions AS sub
ON user.id = sub.user_id
JOIN wine_user_settings AS us
ON us.id = user.id
WHERE sub.wine1 = 'yes'
AND user.receive_mail = 'yes' --- not sure if this line is needed
--- what does `receive_mail` stand for?
AND ( user.24hr = 'no'
OR user.last_mail < NOW() - INTERVAL 1 DAY
OR user.last_mail IS NULL
)