在SQLite中加入三个表

时间:2013-05-17 18:20:22

标签: mysql sql sqlite

我正在编写SQL查询以获取数据,该数据将填充在可滚动表中。

将要比较的内容的内容ID(上图中的Content1,Content2,Content3 ..)将是查询的输入。

因为,为了比较,最少需要2个项目,传递给查询的id数量将始终为2或大于2.

以下是3个表的SQL,从中获取所需的数据。

下表包含要比较的参数名称:

CREATE TABLE tbl_content_commons (
id integer PRIMARY KEY,
content_common_code char(20) NOT NULL,
content_common_name char(100) NOT NULL // The comparison label
)

下表包含上表中比较标签(content_common_name char)的代码和内容的Content id(将作为查询的参数传递)

CREATE TABLE tbl_comparison_values (
id integer PRIMARY KEY,
tbl_content_common_id integer NOT NULL,// ID's of the Contents under comparison
userneed_comparison_label_id  integer NOT NULL,// ID of comparison label in the table above
value char(50) NOT NULL// Value corresponding to a comparison label - if it exists for a given content id 
)

最后,包含内容名称(Content1,Content2 ..)的表格,其id作为参数传递给查询

CREATE TABLE userneed_comparison_labels (
id integer PRIMARY KEY,
name  char(50) NOT NULL// Name of the content whose id's are passed through queries. content ID  in the table above
)

我已经做了足够的努力来编写一个查询来获取数据,这些数据可以帮助我填充附加图像中显示的表格,但是没有成功。我可以显示我写的查询,但由于它再次延长了问题,我不会在这里发帖。

有关如何继续的任何指导或帮助,编写此SQL查询将非常感激。

1 个答案:

答案 0 :(得分:2)

这个怎么样......

select c.content_common_name,
l.name,
v.value
from userneed_comparison_labels l
left join tbl_comparison_values v on l.id = v.userneed_comparison_label_id
left join tbl_content_commons c on c.id = v.tbl_content_common_id
where c.id in (1, 2, 3)

有关详细信息,请参阅SQL Fiddle

选择SQLLite(SQL.js)版本。如果询问您是否要使用WebSQL,请单击取消。

相关问题