gpt4 book ai didi

mysql - 在 SQLite 中连接三个表

转载 作者:行者123 更新时间:2023-11-29 02:28:39 25 4
gpt4 key购买 nike

我正在编写一个 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 查询的指导或帮助将不胜感激。

最佳答案

这个怎么样...

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,请单击取消。

关于mysql - 在 SQLite 中连接三个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16615534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com