简体   繁体   中英

SQL - selecting rows where 2 rows must be the same

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?

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:

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

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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM