MySQL选择、连接并设置一个键作为列名-SQL作为
以下是SQLFiddle: 我想连接两个表,并选择meta_键,使其成为meta_值字段的列名。这是我想加入的表格 表一MySQL选择、连接并设置一个键作为列名-SQL作为,mysql,sql,Mysql,Sql,以下是SQLFiddle: 我想连接两个表,并选择meta_键,使其成为meta_值字段的列名。这是我想加入的表格 表一 +----+-------------+-----------------------+ | ID | post_author | post_date | +----+-------------+-----------------------+ | 20 | 1 | '2014-02-13 22:29:04' | | 21 |
+----+-------------+-----------------------+
| ID | post_author | post_date |
+----+-------------+-----------------------+
| 20 | 1 | '2014-02-13 22:29:04' |
| 21 | 1 | '2014-02-13 22:29:04' |
| 22 | 2 | '2014-02-13 22:29:04' |
| 23 | 1 | '2014-02-13 22:29:04' |
| 24 | 2 | '2014-02-13 22:29:04' |
+----+-------------+-----------------------+
表二
+---------+---------+---------------+----------------------------------------+
| meta_id | post_id | meta_key | meta_value |
+---------+---------+---------------+----------------------------------------+
| 42 | 20 | 'user_title' | 'My Title!' |
| 43 | 20 | 'user_upload' | 'true' |
| 44 | 20 | 'user_desc' | 'a description!' |
| 46 | 21 | 'user_title' | 'whats that about!' |
| 47 | 21 | 'user_upload' | 'truth' |
| 48 | 21 | 'user_desc' | 'this table runs' |
| 49 | 22 | 'user_title' | 'third title, dang!' |
| 50 | 22 | 'user_upload' | 'true' |
| 51 | 22 | 'user_desc' | 'the desc is always true in this case' |
| 52 | 23 | 'user_title' | 'another one' |
| 53 | 23 | 'user_upload' | 'true') |
| 54 | 23 | 'user_desc' | 'thrilling' |
| 55 | 23 | 'user_title' | 'lasty last' |
| 57 | 23 | 'user_upload' | 'true' |
| 59 | 23 | 'user_desc' | 'and its done' |
+---------+---------+---------------+----------------------------------------+
这就是我希望它如何处理连接并选择为
+---------+-------------+----------------------+-------------+----------------------------------------+
| post_id | post_author | user_title | user_upload | user_desc |
+---------+-------------+----------------------+-------------+----------------------------------------+
| 20 | 1 | 'My Title!' | 'true' | 'a description!' |
| 21 | 1 | 'whats that about!' | 'truth' | 'this table runs' |
| 22 | 2 | 'third title, dang!' | 'true' | 'the desc is always true in this case' |
| 23 | 1 | 'another one' | 'true' | 'thrilling' |
| 24 | 2 | 'lasty last' | 'true' | 'and its done' |
+---------+-------------+----------------------+-------------+----------------------------------------+
一种方法是使用
连接和条件聚合:
select p.id as post_id, p.post_author,
max(case when pm.meta_key = 'user_title' then pm.meta_value end) as user_title,
max(case when pm.meta_key = 'user_upload' then pm.meta_value end) as user_upload,
max(case when pm.meta_key = 'user_desc' then pm.meta_value end) as user_desc
from posts p left outer join
postmeta pm
on p.id = pm.post_id
group by p.id, p.post_author;
另一种方法是使用联接的:
select p.id as post_id, p.post_author, title.meta_value as user_title,
upload.meta_value as user_upload, desc.meta_value as user_desc
from posts p left outer join
postmeta title
on p.id = title.post_id and title.meta_value = 'user_title' left outer join
postmeta upload
on p.id = title.post_id and title.meta_value = 'user_upload' left outer join
postmeta desc
on p.id = title.post_id and title.meta_value = 'user_desc';
哪个更好取决于几个因素,例如数据的大小、索引以及表的大小
编辑:
要添加上载为“true”的,您可以添加:
having user_upload = 'true'
对其中一个查询。这在第一种情况下起作用,因为分组方式是。它在第二种情况下工作,因为MySQL扩展允许在having
子句中使用列别名进行过滤,即使没有groupby
一种方法是使用join
和条件聚合:
select p.id as post_id, p.post_author,
max(case when pm.meta_key = 'user_title' then pm.meta_value end) as user_title,
max(case when pm.meta_key = 'user_upload' then pm.meta_value end) as user_upload,
max(case when pm.meta_key = 'user_desc' then pm.meta_value end) as user_desc
from posts p left outer join
postmeta pm
on p.id = pm.post_id
group by p.id, p.post_author;
另一种方法是使用联接的:
select p.id as post_id, p.post_author, title.meta_value as user_title,
upload.meta_value as user_upload, desc.meta_value as user_desc
from posts p left outer join
postmeta title
on p.id = title.post_id and title.meta_value = 'user_title' left outer join
postmeta upload
on p.id = title.post_id and title.meta_value = 'user_upload' left outer join
postmeta desc
on p.id = title.post_id and title.meta_value = 'user_desc';
哪个更好取决于几个因素,例如数据的大小、索引以及表的大小
编辑:
要添加上载为“true”的,您可以添加:
having user_upload = 'true'
对其中一个查询。这在第一种情况下起作用,因为分组方式是。它在第二种情况下工作,因为MySQL扩展允许在having
子句中使用列别名进行过滤,即使没有groupby
一种方法是使用join
和条件聚合:
select p.id as post_id, p.post_author,
max(case when pm.meta_key = 'user_title' then pm.meta_value end) as user_title,
max(case when pm.meta_key = 'user_upload' then pm.meta_value end) as user_upload,
max(case when pm.meta_key = 'user_desc' then pm.meta_value end) as user_desc
from posts p left outer join
postmeta pm
on p.id = pm.post_id
group by p.id, p.post_author;
另一种方法是使用联接的:
select p.id as post_id, p.post_author, title.meta_value as user_title,
upload.meta_value as user_upload, desc.meta_value as user_desc
from posts p left outer join
postmeta title
on p.id = title.post_id and title.meta_value = 'user_title' left outer join
postmeta upload
on p.id = title.post_id and title.meta_value = 'user_upload' left outer join
postmeta desc
on p.id = title.post_id and title.meta_value = 'user_desc';
哪个更好取决于几个因素,例如数据的大小、索引以及表的大小
编辑:
要添加上载为“true”的,您可以添加:
having user_upload = 'true'
对其中一个查询。这在第一种情况下起作用,因为分组方式是。它在第二种情况下工作,因为MySQL扩展允许在having
子句中使用列别名进行过滤,即使没有groupby
一种方法是使用join
和条件聚合:
select p.id as post_id, p.post_author,
max(case when pm.meta_key = 'user_title' then pm.meta_value end) as user_title,
max(case when pm.meta_key = 'user_upload' then pm.meta_value end) as user_upload,
max(case when pm.meta_key = 'user_desc' then pm.meta_value end) as user_desc
from posts p left outer join
postmeta pm
on p.id = pm.post_id
group by p.id, p.post_author;
另一种方法是使用联接的:
select p.id as post_id, p.post_author, title.meta_value as user_title,
upload.meta_value as user_upload, desc.meta_value as user_desc
from posts p left outer join
postmeta title
on p.id = title.post_id and title.meta_value = 'user_title' left outer join
postmeta upload
on p.id = title.post_id and title.meta_value = 'user_upload' left outer join
postmeta desc
on p.id = title.post_id and title.meta_value = 'user_desc';
哪个更好取决于几个因素,例如数据的大小、索引以及表的大小
编辑:
要添加上载为“true”的,您可以添加:
having user_upload = 'true'
对其中一个查询。这在第一种情况下起作用,因为分组方式是。它在第二种情况下工作,因为MySQL扩展允许在having
子句中使用列别名进行过滤,即使没有groupby
另一种方法
Declare @posts TABLE
(
ID bigint primary key,
post_author bigint not null,
post_date datetime not null
);
INSERT INTO @posts
(ID, post_author, post_date)
VALUES
(20, 1, '2014-02-13 22:29:04'),
(21, 1, '2014-02-13 22:28:06'),
(22, 2, '2014-02-13 22:26:27'),
(23, 1, '2014-02-13 22:26:08'),
(24, 2, '2014-02-13 22:25:02');
Declare @postmeta TABLE
(
meta_id bigint primary key,
post_id bigint not null,
meta_key varchar(255),
meta_value varchar(255)
);
INSERT INTO @postmeta
(meta_id, post_id, meta_key, meta_value)
VALUES
(42, 20, 'user_title', 'My Title!'),
(43, 20, 'user_upload', 'true'),
(44, 20, 'user_desc', 'a description!'),
(46, 21, 'user_title', 'a different title'),
(47, 21, 'user_upload', 'true'),
(48, 21, 'user_desc', 'this table runs'),
(49, 22, 'user_title', 'third title, dang!'),
(50, 22, 'user_upload', 'true'),
(51, 22, 'user_desc', 'the desc is always true in this case'),
(52, 23, 'user_title', 'another one'),
(53, 23, 'user_upload', 'true'),
(54, 23, 'user_desc', 'thrilling'),
(55, 23, 'user_title', 'lasty last'),
(57, 23, 'user_upload', 'true'),
(59, 23, 'user_desc', 'and its done');
with cte as
(select distinct p.ID ,p.post_author
from @posts p inner join @postmeta pm on p.ID=pm.post_id
)
select id,post_author
,(select top 1 meta_value from @postmeta pm1 where meta_key='user_title' and c.ID=pm1.post_id order by meta_id )
,(select top 1 meta_value from @postmeta pm1 where meta_key='user_upload' and c.ID=pm1.post_id order by meta_id)
,(select top 1 meta_value from @postmeta pm1 where meta_key='user_desc' and c.ID=pm1.post_id order by meta_id )
from cte c
另一种方式,
Declare @posts TABLE
(
ID bigint primary key,
post_author bigint not null,
post_date datetime not null
);
INSERT INTO @posts
(ID, post_author, post_date)
VALUES
(20, 1, '2014-02-13 22:29:04'),
(21, 1, '2014-02-13 22:28:06'),
(22, 2, '2014-02-13 22:26:27'),
(23, 1, '2014-02-13 22:26:08'),
(24, 2, '2014-02-13 22:25:02');
Declare @postmeta TABLE
(
meta_id bigint primary key,
post_id bigint not null,
meta_key varchar(255),
meta_value varchar(255)
);
INSERT INTO @postmeta
(meta_id, post_id, meta_key, meta_value)
VALUES
(42, 20, 'user_title', 'My Title!'),
(43, 20, 'user_upload', 'true'),
(44, 20, 'user_desc', 'a description!'),
(46, 21, 'user_title', 'a different title'),
(47, 21, 'user_upload', 'true'),
(48, 21, 'user_desc', 'this table runs'),
(49, 22, 'user_title', 'third title, dang!'),
(50, 22, 'user_upload', 'true'),
(51, 22, 'user_desc', 'the desc is always true in this case'),
(52, 23, 'user_title', 'another one'),
(53, 23, 'user_upload', 'true'),
(54, 23, 'user_desc', 'thrilling'),
(55, 23, 'user_title', 'lasty last'),
(57, 23, 'user_upload', 'true'),
(59, 23, 'user_desc', 'and its done');
with cte as
(select distinct p.ID ,p.post_author
from @posts p inner join @postmeta pm on p.ID=pm.post_id
)
select id,post_author
,(select top 1 meta_value from @postmeta pm1 where meta_key='user_title' and c.ID=pm1.post_id order by meta_id )
,(select top 1 meta_value from @postmeta pm1 where meta_key='user_upload' and c.ID=pm1.post_id order by meta_id)
,(select top 1 meta_value from @postmeta pm1 where meta_key='user_desc' and c.ID=pm1.post_id order by meta_id )
from cte c
另一种方式,
Declare @posts TABLE
(
ID bigint primary key,
post_author bigint not null,
post_date datetime not null
);
INSERT INTO @posts
(ID, post_author, post_date)
VALUES
(20, 1, '2014-02-13 22:29:04'),
(21, 1, '2014-02-13 22:28:06'),
(22, 2, '2014-02-13 22:26:27'),
(23, 1, '2014-02-13 22:26:08'),
(24, 2, '2014-02-13 22:25:02');
Declare @postmeta TABLE
(
meta_id bigint primary key,
post_id bigint not null,
meta_key varchar(255),
meta_value varchar(255)
);
INSERT INTO @postmeta
(meta_id, post_id, meta_key, meta_value)
VALUES
(42, 20, 'user_title', 'My Title!'),
(43, 20, 'user_upload', 'true'),
(44, 20, 'user_desc', 'a description!'),
(46, 21, 'user_title', 'a different title'),
(47, 21, 'user_upload', 'true'),
(48, 21, 'user_desc', 'this table runs'),
(49, 22, 'user_title', 'third title, dang!'),
(50, 22, 'user_upload', 'true'),
(51, 22, 'user_desc', 'the desc is always true in this case'),
(52, 23, 'user_title', 'another one'),
(53, 23, 'user_upload', 'true'),
(54, 23, 'user_desc', 'thrilling'),
(55, 23, 'user_title', 'lasty last'),
(57, 23, 'user_upload', 'true'),
(59, 23, 'user_desc', 'and its done');
with cte as
(select distinct p.ID ,p.post_author
from @posts p inner join @postmeta pm on p.ID=pm.post_id
)
select id,post_author
,(select top 1 meta_value from @postmeta pm1 where meta_key='user_title' and c.ID=pm1.post_id order by meta_id )
,(select top 1 meta_value from @postmeta pm1 where meta_key='user_upload' and c.ID=pm1.post_id order by meta_id)
,(select top 1 meta_value from @postmeta pm1 where meta_key='user_desc' and c.ID=pm1.post_id order by meta_id )
from cte c
另一种方式,
Declare @posts TABLE
(
ID bigint primary key,
post_author bigint not null,
post_date datetime not null
);
INSERT INTO @posts
(ID, post_author, post_date)
VALUES
(20, 1, '2014-02-13 22:29:04'),
(21, 1, '2014-02-13 22:28:06'),
(22, 2, '2014-02-13 22:26:27'),
(23, 1, '2014-02-13 22:26:08'),
(24, 2, '2014-02-13 22:25:02');
Declare @postmeta TABLE
(
meta_id bigint primary key,
post_id bigint not null,
meta_key varchar(255),
meta_value varchar(255)
);
INSERT INTO @postmeta
(meta_id, post_id, meta_key, meta_value)
VALUES
(42, 20, 'user_title', 'My Title!'),
(43, 20, 'user_upload', 'true'),
(44, 20, 'user_desc', 'a description!'),
(46, 21, 'user_title', 'a different title'),
(47, 21, 'user_upload', 'true'),
(48, 21, 'user_desc', 'this table runs'),
(49, 22, 'user_title', 'third title, dang!'),
(50, 22, 'user_upload', 'true'),
(51, 22, 'user_desc', 'the desc is always true in this case'),
(52, 23, 'user_title', 'another one'),
(53, 23, 'user_upload', 'true'),
(54, 23, 'user_desc', 'thrilling'),
(55, 23, 'user_title', 'lasty last'),
(57, 23, 'user_upload', 'true'),
(59, 23, 'user_desc', 'and its done');
with cte as
(select distinct p.ID ,p.post_author
from @posts p inner join @postmeta pm on p.ID=pm.post_id
)
select id,post_author
,(select top 1 meta_value from @postmeta pm1 where meta_key='user_title' and c.ID=pm1.post_id order by meta_id )
,(select top 1 meta_value from @postmeta pm1 where meta_key='user_upload' and c.ID=pm1.post_id order by meta_id)
,(select top 1 meta_value from @postmeta pm1 where meta_key='user_desc' and c.ID=pm1.post_id order by meta_id )
from cte c
非常感谢。这对我帮助很大。第二段代码使用连接,我听说wordpress使用连接,这部分集成到了连接中。但这段代码在SQLFIDLE上出现了错误。但是我不确定语法错误在哪里。另外,如何在其中添加where子句?我试过使用和user_upload=true,但它告诉我user_upload不是一个列时失败了。谢谢!这对我帮助很大。第二段代码使用连接,我听说wordpress使用连接,这部分集成到了连接中。但这段代码在SQLFIDLE上出现了错误。但是我不确定语法错误在哪里。另外,如何在其中添加where子句?我试过使用和user_upload=true,但它告诉我user_upload不是一个列时失败了。谢谢!这对我帮助很大。第二段代码使用连接,我听说wordpress使用连接,这部分集成到了连接中。但这段代码在SQLFIDLE上出现了错误。但是我不确定语法错误在哪里。另外,如何在其中添加where子句?我试过使用和user_upload=true,但它告诉我user_upload不是一个列时失败了。谢谢!这对我帮助很大。第二段代码使用连接,我听说wordpress使用连接,这部分集成到了连接中。但这段代码在SQLFIDLE上出现了错误。但是我不确定语法错误在哪里。另外,如何在其中添加where子句?我尝试使用和user_upload=true,但它告诉我user_upload不是列时失败。