简体   繁体   English

如何从两个表(其中每个表的一列包含相似的值)中获取值?

[英]How to get values from two tables in which one column in each table contains similar value?

I have two tables the queries are given below 我有两个表,查询如下

db.execSQL("CREATE TABLE IF NOT EXISTS allquestions (num_id INTEGER PRIMARY KEY NOT NULL, questions TEXT NOT NULL,catogery TEXT NOT NULL,age INT NOT NULL)" );
db.execSQL("CREATE TABLE IF NOT EXISTS answers (num_id INTEGER PRIMARY KEY NOT NULL,questionid INT NOT NULL,answer TEXT NOT NULL)" );

在此处输入图片说明

Here the num_id in allqestions contains the question number and the answers are there in the table(answers) column answer.In the answer column the questionid contains the question number for the answer.So what I want is how to write the query to get questions from allquestion and its answers from table answer. 在这里,所有问题中的num_id包含问题号,并且答案在表(answers)列的答案中。在答案列中,questionid包含答案的问题号。所以我想要的是如何编写查询以获取问题从allquestion及其表答案中得到答案。

我认为您需要它。

select questions,answers from questions que,answers ans where que.num_id=ans.questionid;

You have to apply the join on both tables. 您必须在两个表上应用联接。

       SELECT 
      allquestions.`num_id`,allquestions.`questions`,answers.questionid,
       answers.answer from allquestions  JOIN answers ON allquestions.`num_id`
         = answers.questionid 

Try the following. 尝试以下方法。 I hope you are looking for the same. 希望您也一样。
Give a row_number for each group, then use a CASE statement. 为每个组指定一个row_number,然后使用CASE语句。

Query 询问

select t1.questionid,a.questions,
max(case when t1.row_number=1 then t1.answer else 0 end) as answer1,
max(case when t1.row_number=2 then t1.answer else 0 end) as answer2,
max(case when t1.row_number=3 then t1.answer else 0 end) as answer3
from allquestions a
join (
SELECT @row_number:=CASE WHEN @questionid=questionid 
THEN @row_number+1 
ELSE 1 
END AS row_number,
@questionid:=questionid AS questionid,answer
FROM answers, 
(SELECT @row_number:=0,@questionid:='') AS t
ORDER BY questionid
)t1
on a.num_id=t1.questionid
group by t1.questionid;

Fiddle demo 小提琴演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android-如何从字符串资源中获取名称中包含两个或多个相似单词的所有元素 - Android - How to get from string resources all the elements which contains two or more similar words on their names Android从两个表中的两个列中求和数量值,并使用求和值更新其中一个列中的数量值 - Android Sum Quantity values from two Columns in two tables and update the Quantity value in one of the Column with the summed value 如何从数据库表中获取相应的值,在该数据库表中,列表视图中显示了列值之一 - how to get the respective value from a database table where one of the column values are displayed in a list view 如何从两个不同的表中获取值的差异并将值提供给变量? - How to get the difference in values from two different tables and feed the value to a variable? 如何从表中的一列中选择不同的值? - How to select distinct values from one column in table? 如何从sqlite记录的每一列获取价值? - How to get value from each column of an sqlite record? 如何在 Kotlin 中使用包含一些实体表的 @Embedded 表和同样包含实体的 @Embedded 表构建 RoomDB? - How to build RoomDB in Kotlin with @Embedded table which contains some entity tables and the same @Embedded table, which contains entity too? 如何从表的两列中获取最大值(SQL) - How to get highest values from two columns in a table (SQL) 如何将多个值从一个表插入到另一表的一列? - How to insert several values from one table to other table one column? 如何组合相互依赖的 Observables 并从每个对象中获取一个包含值的对象? - How to combine Observables which depends each other and get an object that contain a value from each?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM