MySQL连接基于前4个字符

时间:2018-05-30 15:23:02

标签: mysql join

我尝试根据第一张表格中字段的前4个字符加入第二张表,但我不断收到" Unknown column' questions.first4'在' on条款'"而且我不确定原因:

SELECT questions.id as id, question, answer, correct, SUBSTRING(question,1,4) as first4
FROM questions
LEFT JOIN answers ON answers.correct_answer = questions.first4
WHERE player_name = 'Alpha Squad'
ORDER BY id ASC

(我意识到这些表的结构效率很低,但不幸的是,我必须按原样使用它们)

表:https://pastebin.com/Gur5ufXa

MySQL:https://pastebin.com/FLgWtQmY

提前致谢!

2 个答案:

答案 0 :(得分:1)

SELECT questions.id as id, question, answer, correct, SUBSTRING(question,1,4) as first4
FROM questions
LEFT JOIN answers ON answers.correct_answer = questions.correct
WHERE player_name = 'Alpha Squad'
ORDER BY id ASC

或者下一个特别加入具有匹配的前4个

的那个
SELECT questions.id as id, question, answer, correct, SUBSTRING(question,1,4) as first4
    FROM questions
    LEFT JOIN answers ON SUBSTRING(answers.correct_answer,1,4) = SUBSTRING(question,1,4)
    ORDER BY id ASC

答案 1 :(得分:0)

SELECT questions.id as id, question, answer, correct, SUBSTRING(question,1,4) as first4
FROM questions
LEFT JOIN answers ON answers.correct_answer = SUBSTRING(question,1,4) 
WHERE player_name = 'Alpha Squad'
ORDER BY id ASC

问题是在尝试进行JOIN时,first4不存在。点击此处查看处理选择状态的订单。

Order Of Execution of the SQL query