很难解释,我的英语没有帮助,但它是: 我有两个数据表:“节点”和“记录”。 在节点表中,我保存了问题数据和答案数据
节点: | id |名字|类型|
记录: | id | questionid | answerid |
我试过尽管没有成功,但是做一个sql查询给了我所有记录的数据,但是带有答案的名称和id的问题内容。 简而言之,我需要的是:records.id,nodes.name(question),nodes.name(answer)
答案 0 :(得分:2)
SELECT
questions.name AS questionname
answers.name AS answername
FROM records
INNER JOIN nodes AS questions ON records.questionid=questions.id
INNER JOIN nodes AS answers ON records.answerid=answers.id
答案 1 :(得分:1)
您可以使用此查询:
SELECT q.name AS question, a.name AS answer
FROM records r
LEFT JOIN nodes q ON q.id = r.questionid
LEFT JOIN nodes a ON a.id = r.answerid
WHERE 1
对于此查询,我在下面构建架构:
CREATE TABLE nodes (
id int auto_increment primary key,
name varchar(30),
type int(1));
INSERT INTO nodes (name, type) VALUES
('Is it a question 01', 0),
('Is it a question 02', 0),
('This is an answer 01', 1),
('This is an answer 02', 1),
('This is an answer 03', 1);
CREATE TABLE records (
id int auto_increment primary key,
questionid int(11),
answerid int(11));
INSERT INTO records (questionid, answerid) VALUES
(1, 3), (1, 4), (1, 5), (2, 4), (2, 5);