【问题标题】:Select first distinct rows on two columns在两列上选择第一个不同的行
【发布时间】:2022-12-04 11:14:25
【问题描述】:

I have a table similar to below

| id | phone        | email            | dob       |
| -- | ----------   | ----             | ----      |
| 1  | 04-------61  | example@gmail.com| 196949200 |
| 2  | 04-------61  | example@gmail.com| 296949200 |
| 3  | 55-------51  | example@gmail.com| 396949200 |
| 4  | 04-------61  | test@gmail.com   | 496949200 |
| 5  | 66-------61  | test@gmail.com   | 596949200 |
| 6  | 77-------61  | another@gmail.com| 696949200 |

I want to get rows that have a distinct \'phone\' and \'email\' (not a distinct combination) along with the data associated with the first instance (here dob as an example).

My desired output:

| id | phone        | email            | dob       |
| -- | ----------   | ----             | ----      |
| 1  | 04-------61  | example@gmail.com| 196949200 |
| 5  | 66-------61  | test@gmail.com   | 596949200 |
| 6  | 77-------61  | another@gmail.com| 696949200 |

Is there a way I can achieve this? I\'ve tried using distinct and group by but I can\'t see how they would be helpful in this case.

  • Please provide DDL + DML commands
  • If distinct on phone and email, your result for phone = 04-----61 will definitely be 2 rows, one is for example@gmail.com, another is test@gmail.com

标签: mysql sql


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 2015-05-18
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
  • 2016-11-23
  • 1970-01-01
  • 2016-08-29
  • 2022-06-14
相关资源
最近更新 更多