SQL - 选择两行必须相同的行
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要查找聚合函数:
You want to look up aggregate functions:
我在没有实际了解架构的情况下编写此内容,因此它可能无法按原样工作:
如果您需要确保至少包含 2 行,请添加:
I'm writing this up without actual knowledge of the schema, so it may not work as is:
If you need to ensure you have at least 2 rows included, add:
如果您的表包含 20 行,其中 2 行相同
即可检索数据
,那么您只需编写
if your table contains 20 rows out of them 2 are same
then you can retrieve the data
by simply writing