使用 MYSQL IF SELECT 2 个不同的数据集
本质上,我想要做的是根据登录时的用户状态对显示的数据进行选择,例如,如果用户是试用用户,则选择特定日期范围内的数据,但如果他们是付费用户,则选择更大的数据日期范围。我尝试了下面的代码,但它抛出一个错误,指出我的语法不正确。任何帮助将不胜感激。
IF (SELECT MemberStatus FROM user WHERE Memberstatus = 'Trial')
THEN
SELECT *
FROM article
WHERE date > '1990/01/01';
ELSE
SELECT *
FROM article
WHERE date > '2000/01/01';
ENDIF;
Essentially what I want to do is make a selection on data displayed based upon a users status when logged in, for example if the user is a trial user then select data from a specific date range but if they are a paid user then select a greater date range. I have tried the following code below but it throws up an error saying my syntax is incorrect. Any help would be greatly appreciated.
IF (SELECT MemberStatus FROM user WHERE Memberstatus = 'Trial')
THEN
SELECT *
FROM article
WHERE date > '1990/01/01';
ELSE
SELECT *
FROM article
WHERE date > '2000/01/01';
ENDIF;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为在一个查询中执行此操作(检查用户状态并根据其选择项目)是错误的。您可能需要使用试用限制或在代码中的其他位置显示试用状态。因此,最好将其存储在用户配置文件或变量的实例中,并构建有关用户状态的查询。
I think it's wrong to do this (check user status and select items depending on it) in one query. You'll probably need to use trial restrictions or display trial status somewhere else in your code. So better to store it in instance of user's profile or a varialble and build queries regarding user's status.
你不需要 IF。这也是标准的 SQL
编辑:根据问题的评论,用户的 SELECT 可能不完整
You don't need an IF. This is also standard SQL
Edit: as per comment to question, the SELECT from user is probably incomplete