1

到目前为止,我有这个:

SELECT
    l.logo_id,
    l.logo_image_path,
    l.logo_lang,
    (1) AS answered
FROM logos l
WHERE l.logo_level = $level"

如果用户回答了这个问题,我想回答显示 1,如果用户没有回答这个问题,我想显示 0。结果存储在包含 r.logo_id r.user_id 的结果表中

我试过了:

(CASE WHEN l.logo_id = r.logo_id THEN 1 ELSE 0 END) AS answered

但它不起作用,显然我缺少结果表,并且 user_id 检查因为我希望当前用户发生这种情况,所以我必须在某处添加 WHERE r.user_id =$user_data[0]['user_id']

你能帮我解决这个问题吗?并且一步一步地解释,所以我终于明白了:)

泰。

4

2 回答 2

0

假设 logos 是您的问题表,结果是您的答案表,每个用户在结果表上有 0 到多行(每个已回答的问题一个),然后是这样的:-

$sql = "SELECT
    l.logo_id,
    l.logo_image_path,
    l.logo_lang,
    CASE WHEN r.logo_id IS NULL THEN 0 ELSE 1 END AS answered
FROM logos l
LEFT OUTER JOIN results r
ON l.logo_id = r.logo_id
AND  r.user_id = ".$user_data[0]['user_id']."
WHERE l.logo_level = ".$level;
于 2013-03-07T12:52:09.513 回答
0
SELECT (CASE WHEN l.logo_id = r.logo_id THEN 1 ELSE 0 END) AS answered, l.logo_id,
    l.logo_image_path,
    l.logo_lang FROM logos l, results r WHERE l.logo_level = $level AND ... clause to join two tables ... AND  r.user_id = $user_data[0]['user_id']
于 2013-03-07T12:46:58.327 回答