2

我正在尝试在 SQL (MySQL) 中做一些我不确定是否可行的事情。该场景与用户调查有关,我正在尝试创建一个调查,其中在一段时间后重新提出问题。我的 SQL 今天看起来像这样(我不得不在内部选择语句中对问题 ID 进行硬编码,因为我不知道该怎么做)。

SELECT surveyquestions.priority, surveyquestions.id, surveyquestions.question,       surveyquestions.html FROM surveyquestions
    LEFT JOIN surveyanswers ON surveyquestions.id = surveyanswers.id AND surveyanswers.uuid = 22946252362612764
    WHERE (surveyanswers.id IS null OR 
    (SELECT datestamp FROM surveyanswers WHERE id = 22 ORDER BY datestamp DESC LIMIT 1) < 
    DATE_SUB(NOW(),INTERVAL 1 HOUR))
    AND surveyquestions.priority != 999 
    AND surveyquestions.brid = 'testprod2'
    ORDER BY surveyquestions.priority LIMIT 1;

我想以某种方式使用外部 SELECT 中的surveyquestion.id 作为内部选择 where 语句的一部分(请参阅 WHERE id = 22 是硬编码位)。

编辑:声明的更多信息/背景
- 优先级是问题出现的顺序
- 优先级 999 表示不显示问题
- brid 是与问题相关的品牌/产品
- uuid 是该问题的唯一标识符回答问题的用户

4

1 回答 1

4

为了在表名相同时执行此操作,您需要为表指定别名

SELECT surveyquestions.priority, surveyquestions.id, surveyquestions.question,       surveyquestions.html FROM surveyquestions
    LEFT JOIN surveyanswers oa ON surveyquestions.id = oa.id AND oa.uuid = 22946252362612764
    WHERE (surveyanswers.id IS null OR 
    (SELECT datestamp FROM surveyanswers ia WHERE ia.id = oa.id ORDER BY datestamp DESC LIMIT 1) < 
    DATE_SUB(NOW(),INTERVAL 1 HOUR))
    AND surveyquestions.priority != 999 
    AND surveyquestions.brid = 'testprod2'
    ORDER BY surveyquestions.priority LIMIT 1;

我曾经oa表示“外部答案”并ia表示“内部答案”。

于 2013-06-26T11:00:58.460 回答