SQL - 选择两行必须相同的行

发布于 2024-12-09 11:57:53 字数 778 浏览 2 评论 0原文

我对 SQL 非常缺乏经验。我有一个看起来像这样的表:

Columns:   A   B   C   D   E
           foo bar 1   2   3
           foo bar 4   5   6
           foo bar 7   8   9
           xyz abc 3   2   1
           xyz abc 6   5   4
           xyz abc 9   8   7

现在我希望能够形成一个像这样的字符串:

"foo bar: 1   2   3   4   5   6   7   8   9"
"xyz abc: 3   2   1   6   5   4   9   8   7"

如果重要的话,我还有一个 A 和 B 列的列表,我可以通过以下方式天真地使用:

Rs1 = SELECT * FROM PARENT_TABLE:
    for a, b in RS1
        String = a + b
        Rs2 = SELECT C, D, E FROM CHILD_TABLE WHERE A='a' AND B='b'
            for every row in Rs:
                String += C D E
        print String

有没有办法做到这一点必须遍历父表,然后在每一行上形成一个语句,从而也对该行进行迭代。我错过了一个明显的解决方案吗?

I am very inexperienced with SQL. I have a table that looks like this:

Columns:   A   B   C   D   E
           foo bar 1   2   3
           foo bar 4   5   6
           foo bar 7   8   9
           xyz abc 3   2   1
           xyz abc 6   5   4
           xyz abc 9   8   7

Now I want to be able to form a string like so:

"foo bar: 1   2   3   4   5   6   7   8   9"
"xyz abc: 3   2   1   6   5   4   9   8   7"

If it matters I also have a list of the A and B columns I can use naively by going:

Rs1 = SELECT * FROM PARENT_TABLE:
    for a, b in RS1
        String = a + b
        Rs2 = SELECT C, D, E FROM CHILD_TABLE WHERE A='a' AND B='b'
            for every row in Rs:
                String += C D E
        print String

Is there anyway to do this WITHOUT having to iterate through the parent table and then on each row form a statement and thus iterate on that one as well. Am I missing an obvious solution?

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

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

发布评论

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

评论(3

痴情 2024-12-16 11:57:53

我在没有实际了解架构的情况下编写此内容,因此它可能无法按原样工作:

SELECT A || ' ' || B || ': ' || WM_CONCAT(D || ' ' || E || ' ' || F || ' ')
  FROM PARENT_TABLE PT 
  INNER JOIN CHILD_TABLE CT ON CT.A=PT.A AND CT.B=PT.B
  GROUP BY (PT.A,PT.B)

如果您需要确保至少包含 2 行,请添加:

  HAVING COUNT(PT.A,PT.B)>=2

I'm writing this up without actual knowledge of the schema, so it may not work as is:

SELECT A || ' ' || B || ': ' || WM_CONCAT(D || ' ' || E || ' ' || F || ' ')
  FROM PARENT_TABLE PT 
  INNER JOIN CHILD_TABLE CT ON CT.A=PT.A AND CT.B=PT.B
  GROUP BY (PT.A,PT.B)

If you need to ensure you have at least 2 rows included, add:

  HAVING COUNT(PT.A,PT.B)>=2
莳間冲淡了誓言ζ 2024-12-16 11:57:53

如果您的表包含 20 行,其中 2 行相同

即可检索数据

,那么您只需编写

select * from tablename where column_name='common value of that column';

if your table contains 20 rows out of them 2 are same

then you can retrieve the data

by simply writing

select * from tablename where column_name='common value of that column';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文