具有动态字段的 SQL

发布于 2024-12-22 20:32:20 字数 2121 浏览 0 评论 0原文

我有 4 个表:

CREATE TABLE Category (
    Id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    Name VARCHAR(255) NOT NULL,
) Engine=InnoDB;

CREATE TABLE Category_Template (
    Id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    Name VARCHAR(255) NOT NULL,
    Category_Id INT UNSIGNED NOT NULL,
    FOREIGN KEY (Category_Id) REFERENCES Category (Id)
) Engine=InnoDB;

CREATE TABLE Post (
    Id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    Post_Name VARCHAR(255) NOT NULL,
    Category_Id INT UNSIGNED NOT NULL,
    FOREIGN KEY (Category_Id) REFERENCES Category (Id)
) Engine=InnoDB;

CREATE TABLE Post_Fields (
    Id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    Post_Id INT UNSIGNED NOT NULL,
    Category_Template_Id INT UNSIGNED NOT NULL,
    Value VARCHAR(255) NOT NULL,
    FOREIGN KEY (Category_Template_Id) REFERENCES Category_Template (Id)
) Engine=InnoDB;

例如,假设有一个属于旅行类别的帖子:

INSERT INTO Category 
(Id, Name) 
  VALUES
(1, 'Where to go?')
;

INSERT INTO Category_Template
(Category_Template_Id, Name,       Category_Id)
  VALUES
(1,                    'Budget',   1),
(2,                    'Location', 1),
(3,                    'Time',     1)
;

INSERT INTO Post
(PostId, Post_Name,        Category_Id)
  VALUES
(1,     'America is good', 1),
(2,     'Hot Travel',      1)
;

INSERT INTO Post_Fields 
(Id, Post_Id, Category_Template_Id, Value)
  VALUES
(1,  1,       1,                    '1000 $'),
(1,  1,       2,                    'New York'),
(1,  1,       3,                    'January 2012'),
(1,  2,       1,                    '2000 $'),
(1,  2,       2,                    'Brasil'),
(1,  2,       3,                    'May 2012')
;

我想运行一个 SQL 查询,它将列出属于给定类别的所有帖子,并将这些帖子的所有字段作为单独的列列出,其中类别模板名称作为列名称。结果应该是这样的:

帖子 ID 帖子名称 预算 地点 时间
1 美国很好 1000 美元 纽约 2012 年 1 月
2 Hot Travel 2000 $ 巴西 2012 年 5 月

我的问题是不同类别的类别模板具有不同的名称,这使得编写单个通用查询变得困难。例如,旅行类别可以有 3 个模板(位置、预算、时间),而书籍类别有 4 个模板(摘要、内容、作者简介、摘要)。当输出列的确切数量发生变化时,如何编写将一列转换为多列的语句(基于同一行但另一列中的值)?同样,考虑到输出列的数量会有所不同,如何编写将一列转换为多个列标题的语句?

I have 4 tables:

CREATE TABLE Category (
    Id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    Name VARCHAR(255) NOT NULL,
) Engine=InnoDB;

CREATE TABLE Category_Template (
    Id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    Name VARCHAR(255) NOT NULL,
    Category_Id INT UNSIGNED NOT NULL,
    FOREIGN KEY (Category_Id) REFERENCES Category (Id)
) Engine=InnoDB;

CREATE TABLE Post (
    Id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    Post_Name VARCHAR(255) NOT NULL,
    Category_Id INT UNSIGNED NOT NULL,
    FOREIGN KEY (Category_Id) REFERENCES Category (Id)
) Engine=InnoDB;

CREATE TABLE Post_Fields (
    Id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    Post_Id INT UNSIGNED NOT NULL,
    Category_Template_Id INT UNSIGNED NOT NULL,
    Value VARCHAR(255) NOT NULL,
    FOREIGN KEY (Category_Template_Id) REFERENCES Category_Template (Id)
) Engine=InnoDB;

As an example, assume there's a post belonging to the travel category:

INSERT INTO Category 
(Id, Name) 
  VALUES
(1, 'Where to go?')
;

INSERT INTO Category_Template
(Category_Template_Id, Name,       Category_Id)
  VALUES
(1,                    'Budget',   1),
(2,                    'Location', 1),
(3,                    'Time',     1)
;

INSERT INTO Post
(PostId, Post_Name,        Category_Id)
  VALUES
(1,     'America is good', 1),
(2,     'Hot Travel',      1)
;

INSERT INTO Post_Fields 
(Id, Post_Id, Category_Template_Id, Value)
  VALUES
(1,  1,       1,                    '1000 

I want to run an SQL query which will list all posts that belong to a given category and list all fields for those posts as separate columns, with the category template names as column names. The results should be something like:

Post Id     Post_Name         Budget         Location        Time
1           America is good   1000 $         New York        January 2012
2           Hot Travel        2000 $         Brasil          May 2012

My problem is that category templates for different categories have different names, making it hard to write a single general use query. For example, a category for traveling can have 3 templates (location, budget, time) whereas a category for books have 4 templates (abstract, content, author_bio, summary). How can I write a statement that turns one column into multiple columns (based on the value in the same row but in another column), when the exact number of output columns will vary? Similarly, how can I write a statement that turns one column into multiple column headers, considering that the number of output columns will vary?

), (1, 1, 2, 'New York'), (1, 1, 3, 'January 2012'), (1, 2, 1, '2000

I want to run an SQL query which will list all posts that belong to a given category and list all fields for those posts as separate columns, with the category template names as column names. The results should be something like:

My problem is that category templates for different categories have different names, making it hard to write a single general use query. For example, a category for traveling can have 3 templates (location, budget, time) whereas a category for books have 4 templates (abstract, content, author_bio, summary). How can I write a statement that turns one column into multiple columns (based on the value in the same row but in another column), when the exact number of output columns will vary? Similarly, how can I write a statement that turns one column into multiple column headers, considering that the number of output columns will vary?

), (1, 2, 2, 'Brasil'), (1, 2, 3, 'May 2012') ;

I want to run an SQL query which will list all posts that belong to a given category and list all fields for those posts as separate columns, with the category template names as column names. The results should be something like:

My problem is that category templates for different categories have different names, making it hard to write a single general use query. For example, a category for traveling can have 3 templates (location, budget, time) whereas a category for books have 4 templates (abstract, content, author_bio, summary). How can I write a statement that turns one column into multiple columns (based on the value in the same row but in another column), when the exact number of output columns will vary? Similarly, how can I write a statement that turns one column into multiple column headers, considering that the number of output columns will vary?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稳稳的幸福 2024-12-29 20:32:20
SELECT Post_Id as `Post Id`, Post_Name,
    (SELECT Value FROM Post_Fields WHERE Category_Template_Id=1 
          AND Post_Fields.Post_Id=Post.Post_id) AS `Budget`,
    ... more of the same ...
FROM Post WHERE Post_Id = 1
SELECT Post_Id as `Post Id`, Post_Name,
    (SELECT Value FROM Post_Fields WHERE Category_Template_Id=1 
          AND Post_Fields.Post_Id=Post.Post_id) AS `Budget`,
    ... more of the same ...
FROM Post WHERE Post_Id = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文