Select everything from SQL database except duplicate

kai

I have a database that looks like this:

handshakes
-----------------------------------
|id | participant1 | participant2 |
-----------------------------------
| 1 | Thomas Miller| Max Miller   |
| 2 | Thomas Miller| Jack Miller  |
| 3 | Jack Miller  | Max Miller   |
| 4 | Max Miller   | Thomas Miller|
| 5 | Jack Miller  | Max Miller   |
-----------------------------------

It measures how many times participant1 and participant2 have shaked hands.
I want to select the amount of times total people have shaken hands (without the duplicates counted).
So in this example the output would be like this:

Thomas Miller | Max Miller
Thomas Miller | Jack Miller
Jack miller   | Max Miller
Total: 3times

Can anyone help me with the SQL Statement to do so?

forpas

With NOT EXISTS:

select id, h.participant1, h.participant2 
from handshakes h
where not exists (
  select 1 from handshakes
  where id < h.id 
  and least(participant1, participant2) = least(h.participant1, h.participant2) 
  and greatest(participant1, participant2) = greatest(h.participant1, h.participant2) 
)

This query uses the functions least() and greatest() which are supported by MySql and Postgresql.

See the demo.
Results:

| id  | participant1  | participant2 |
| --- | ------------- | ------------ |
| 1   | Thomas Miller | Max Miller   |
| 2   | Thomas Miller | Jack Miller  |
| 3   | Jack Miller   | Max Miller   |

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

excluding everything from git except

分類Dev

Matrix - Select everything except specified indices

分類Dev

SQL:How to select the first record from duplicate rows?

分類Dev

SQL:How to select the first record from duplicate rows?

分類Dev

CSS :not() selector - Hide everything from body except one div

分類Dev

Remove everything from file names except for a particular part taken by RegEx

分類Dev

Everything gone except desktop

分類Dev

Redirect everything except images

分類Dev

Select all options Except one from dropdown

分類Dev

Remove everything except numbers and alphabets from a string using google sheet or excel formulas

分類Dev

Unable to select data from database

分類Dev

Import values from database to a select

分類Dev

Why doesn't my SQL query select any row from the sqllite database

分類Dev

.gitignore - Ignore everything in a directory except one file

分類Dev

Regex to match everything except strings that starts with digits

分類Dev

Regex that match everything except the list of strings

分類Dev

Replace everything with spaces except line terminators

分類Dev

Regex Notepad ++: How to remove everything except url?

分類Dev

Remove everything except for the URL's in Notepad++

分類Dev

How to replace everything except a specific pattern with sed?

分類Dev

SQL Server insert EXCEPT without selecting from a table

分類Dev

duplicate entries while inserting data from jtable to derby database

分類Dev

Room database, SELECT * FROM table by a given day

分類Dev

select list from database is giving null

分類Dev

jQuery display table from database dynamically with select

分類Dev

Select table from database where value is X

分類Dev

Select top score only from MySQL database

分類Dev

Basic SELECT query - results from multiple database

分類Dev

How to select from an expression in SQL?

Related 関連記事

  1. 1

    excluding everything from git except

  2. 2

    Matrix - Select everything except specified indices

  3. 3

    SQL:How to select the first record from duplicate rows?

  4. 4

    SQL:How to select the first record from duplicate rows?

  5. 5

    CSS :not() selector - Hide everything from body except one div

  6. 6

    Remove everything from file names except for a particular part taken by RegEx

  7. 7

    Everything gone except desktop

  8. 8

    Redirect everything except images

  9. 9

    Select all options Except one from dropdown

  10. 10

    Remove everything except numbers and alphabets from a string using google sheet or excel formulas

  11. 11

    Unable to select data from database

  12. 12

    Import values from database to a select

  13. 13

    Why doesn't my SQL query select any row from the sqllite database

  14. 14

    .gitignore - Ignore everything in a directory except one file

  15. 15

    Regex to match everything except strings that starts with digits

  16. 16

    Regex that match everything except the list of strings

  17. 17

    Replace everything with spaces except line terminators

  18. 18

    Regex Notepad ++: How to remove everything except url?

  19. 19

    Remove everything except for the URL's in Notepad++

  20. 20

    How to replace everything except a specific pattern with sed?

  21. 21

    SQL Server insert EXCEPT without selecting from a table

  22. 22

    duplicate entries while inserting data from jtable to derby database

  23. 23

    Room database, SELECT * FROM table by a given day

  24. 24

    select list from database is giving null

  25. 25

    jQuery display table from database dynamically with select

  26. 26

    Select table from database where value is X

  27. 27

    Select top score only from MySQL database

  28. 28

    Basic SELECT query - results from multiple database

  29. 29

    How to select from an expression in SQL?

ホットタグ

アーカイブ