Selecting records with the same name give duplicate results

bZhang

I have a query that scans for people with same name but different ids. Table structure is Staff(name,id)

What I want to find is people who share the same name but with different id(they are different people).

I do happen to have two people with same name & diff id.

+---------+-----+
|  NAME   | ID  |
+---------+-----+
| John S. | 138 |
| John S. | 491 |
+---------+-----+

so far I have

 select a.name, b.name, a.id, b.id
 from staff a, staff b
 where a.name = b.name and a.id != b.id

But when I run this code it gives the output twice, which are

+---------+-----+
|  NAME   | ID  |
+---------+-----+
| John S. | 138 |
| John S. | 491 |
| John S. | 491 |
| John S. | 138 |
+---------+-----+

I know why this happens because these two outputs both satisfy the checking condition, but is there anyway I can suppress ones that are already outputted? I can run a select table and WHERE ROWNUM <= 2 but that wont be the optimal case when I have more people with same names.

Thanks!

sagi

If you want only one result you can do something like this:

 select a.name, b.name, a.id, b.id
 from staff a, staff b
 where a.name = b.name and a.id > b.id

This way, only one of the combinations between them will answer the join condition, therefore , only one will be returned

BTW - please avoid the use of implicit join syntax's(comma separated) . Use only the explicit syntax of join, like this:

 SELECT a.name, b.name, a.id, b.id
 FROM staff a
 INNER JOIN staff b
  ON(a.name = b.name and a.id > b.id)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Get either of the entry in duplicate records

来自分类Dev

How to go back to the results of :Gblame after selecting a particular commit?

来自分类Dev

Why does this example from the myHDL manual give me different results?

来自分类Dev

Why do float literals and variables give different results in Go?

来自分类Dev

How do I best avoid inserting duplicate records in CakePHP?

来自分类Dev

Testing Functions with the same name in GO

来自分类Dev

Two Operations with the same name in the Service

来自分类Dev

foreach from=$results item="entry" name=status

来自分类Dev

Gradle and Android Studio Duplicate File Copied from same jar

来自分类Dev

Have the same Java file name as the project name in intelliJ

来自分类Dev

Primefaces, autocomplete, multiple mode. How to avoid selecting same item twice?

来自分类Dev

BASH : Sum size of same name directories

来自分类Dev

Laravel 4 - JOIN - Same column name

来自分类Dev

Show / Hide divs individually with same class name

来自分类Dev

Replace column if the same column name R

来自分类Dev

RestKit XML Mapping with same element and attribute name

来自分类Dev

Set element name same as RootElement attribute

来自分类Dev

CheerioJS, looping through <ul> with same class name

来自分类Dev

How to move directory into a directory with the same name?

来自分类Dev

更改主机后,give_profile_name()错误缺少参数1

来自分类Dev

How can I count the Mongo records that match a query or See if there are any results to a query?

来自分类Dev

how to change values with rank value (ordered value) in R? and I want to give same numbers when two values tie

来自分类Dev

How to call a script function with same div name in php?

来自分类Dev

Grunt: Destination File Same Name as Source File After Templating

来自分类Dev

EmberJs router with two route segments with the same name - possible?

来自分类Dev

May static and non static variables have the same name

来自分类Dev

Upload apk with different signature and same package name in google play store

来自分类Dev

Why can't I have a folder and a file with the same name?

来自分类Dev

Clicking a button to run casperJS in PHP and echo results in same PHP page with button

Related 相关文章

  1. 1

    Get either of the entry in duplicate records

  2. 2

    How to go back to the results of :Gblame after selecting a particular commit?

  3. 3

    Why does this example from the myHDL manual give me different results?

  4. 4

    Why do float literals and variables give different results in Go?

  5. 5

    How do I best avoid inserting duplicate records in CakePHP?

  6. 6

    Testing Functions with the same name in GO

  7. 7

    Two Operations with the same name in the Service

  8. 8

    foreach from=$results item="entry" name=status

  9. 9

    Gradle and Android Studio Duplicate File Copied from same jar

  10. 10

    Have the same Java file name as the project name in intelliJ

  11. 11

    Primefaces, autocomplete, multiple mode. How to avoid selecting same item twice?

  12. 12

    BASH : Sum size of same name directories

  13. 13

    Laravel 4 - JOIN - Same column name

  14. 14

    Show / Hide divs individually with same class name

  15. 15

    Replace column if the same column name R

  16. 16

    RestKit XML Mapping with same element and attribute name

  17. 17

    Set element name same as RootElement attribute

  18. 18

    CheerioJS, looping through <ul> with same class name

  19. 19

    How to move directory into a directory with the same name?

  20. 20

    更改主机后,give_profile_name()错误缺少参数1

  21. 21

    How can I count the Mongo records that match a query or See if there are any results to a query?

  22. 22

    how to change values with rank value (ordered value) in R? and I want to give same numbers when two values tie

  23. 23

    How to call a script function with same div name in php?

  24. 24

    Grunt: Destination File Same Name as Source File After Templating

  25. 25

    EmberJs router with two route segments with the same name - possible?

  26. 26

    May static and non static variables have the same name

  27. 27

    Upload apk with different signature and same package name in google play store

  28. 28

    Why can't I have a folder and a file with the same name?

  29. 29

    Clicking a button to run casperJS in PHP and echo results in same PHP page with button

热门标签

归档