sqlite3:如果匹配,如何选择同一行?

时间:2015-04-29 06:15:07

标签: sqlite

创建SOF.SQL

CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US');
INSERT INTO "android_metadata" VALUES ('en_US');

CREATE TABLE main.t_def (
 _id     INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
 word      TEXT(20) not null,
 word_def  TEXT(20) not null
);

insert into t_def (word, word_def) values ('ball','spherical object');
insert into t_def (word, word_def) values ('cat','feline');
insert into t_def (word, word_def) values ('dog','common housekept');

CREATE TABLE main.t_a (
  _id        INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  corr_answer      TEXT(20) not null,
  user_answer      TEXT(20) not null,
  is_correct       INTEGER not null
);

insert into t_a (user_answer, corr_answer, is_correct) values ('ball','cat',0);
insert into t_a (user_answer, corr_answer, is_correct) values ('dog','dog',1);
.exit

然后运行:

sqlite3 foo.db < SOF.SQL

我想要一个结果集:

 ball|spherical object|cat|feline|0

这是我最接近的:

 select t_def.word, t_def.word_def from t_def, t_a where t_a.is_correct=0 and t_a.corr_answer=t_def.word;

1 个答案:

答案 0 :(得分:1)

要从两行获取值,您需要表的两个实例:

SELECT t_a.user_answer,
       user_def.word_def AS user_word_def,
       t_a.corr_answer,
       corr_def.word_def AS corr_word_def,
       t_a.is_correct
FROM t_a
JOIN t_def AS user_def ON t_a.user_answer = user_def.word
JOIN t_def AS corr_def ON t_a.corr_answer = corr_def.word
WHERE NOT t_a.is_correct