我可以将一个表行的结果合并为一行(带列)吗?

发布于 2024-10-17 00:56:19 字数 746 浏览 2 评论 0原文

我本质上是想完成以下任务:

表 1:

MemberID   |    selection 1     |    selection 2     |     selection 3
-----------------------------------------------------------------------
12345      |        A           |                    |                 
12345      |                    |       B            |                   
12345      |                    |                    |          C

转换为

表 2:

MemberID   |    selection 1     |    selection 2     |     selection 3
-----------------------------------------------------------------------
12345      |        A           |       B            |        C        

我认为“UNION”是可行的方法,但我认为这通常是为将列转换为行而保留的。

有什么想法吗?

I am essentially trying to accomplish this following:

Table 1:

MemberID   |    selection 1     |    selection 2     |     selection 3
-----------------------------------------------------------------------
12345      |        A           |                    |                 
12345      |                    |       B            |                   
12345      |                    |                    |          C

Transforms into

Table 2:

MemberID   |    selection 1     |    selection 2     |     selection 3
-----------------------------------------------------------------------
12345      |        A           |       B            |        C        

Part of me thinks a 'UNION' is the way to go but this is normally reserved for columns into rows I believe.

Any ideas?

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

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

发布评论

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

评论(2

很糊涂小朋友 2024-10-24 00:56:19

我的第一个想法是聚合技巧。没有数据的列是 NULL 还是空白?也许这些值排序在空白列的上方或下方。在这种情况下:

SELECT MAX(column1),MAX(column2),MAX(column3) FROM table GROUP BY MemberID;

My first thought is an aggregation trick. Are the columns without data NULL or just blank? Perhaps the values sort above or below the blank columns. In that case:

SELECT MAX(column1),MAX(column2),MAX(column3) FROM table GROUP BY MemberID;

咿呀咿呀哟 2024-10-24 00:56:19

我认为你试图在一张桌子上承担两项职责。 表

表 1、成员

id

表 2、选择

id
member_id
selection

数据将如下所示:

1(成员)

id
----
12345

表 2(选择)

id    member_id    selection
--------------------------------
1     12345        a
2     12345        b
3     12345        c

那么您将获得所有选择,每行一个,

SELECT members.id,
       selections.selection
FROM   members
JOIN   selections
ON     members.id = selections.member_id

或者如果您只想要一个成员的选择

SELECT member_id AS id,
       selection
FROM   selections
WHERE  member_id = ?

i'm thinking you're trying to have two responsibilities in one table. how about

table 1, members

id

table 2, selections

id
member_id
selection

data would look like this:

table 1 (members)

id
----
12345

table 2 (selections)

id    member_id    selection
--------------------------------
1     12345        a
2     12345        b
3     12345        c

then you would get all selections, one per row, by doing

SELECT members.id,
       selections.selection
FROM   members
JOIN   selections
ON     members.id = selections.member_id

or if you just want the selections of one member

SELECT member_id AS id,
       selection
FROM   selections
WHERE  member_id = ?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文