[英]SQL - selecting rows where 2 rows must be the same
I am very inexperienced with SQL. 我对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: 如果重要的话我还有一个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
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? 我错过了明显的解决方案吗?
You want to look up aggregate functions: 您想要查找聚合函数:
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: 如果您需要确保至少包含2行,请添加:
HAVING COUNT(PT.A,PT.B)>=2
if your table contains 20 rows out of them 2 are same 如果你的表中包含20行,则它们是相同的
then you can retrieve the data 然后你可以检索数据
by simply writing 只需写作
select * from tablename where column_name='common value of that column';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.