Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

select and group by same value mysql

i have table with data like this below

| id | wallet_id | wallet_name | deposit |   |
|----|-----------|-------------|---------|---|
| 1  | 12        | a_wallet    | 10      |   |
| 2  | 14        | c_wallet    | 12      |   |
| 3  | 12        | a_wallet    | 24      |   |
| 4  | 15        | e_wallet    | 50      |   |
| 5  | 14        | c_wallet    | 10      |   |
| 6  | 15        | e_wallet    | 22      |   |

i want to select and group with same wallet_id, probably something like this

| wallet_id | id | wallet_name |
|-----------|----|-------------|
| 12        | 1  | a_wallet    |
|           | 3  | a_wallet    |
| 14        | 2  | c_wallet    |
|           | 5  | c_wallet    |
| 15        | 4  | e_wallet    |
|           | 6  | e_wallet    |

i already try

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

select wallet_id, id, wallet_name from wallet group by wallet_id

but it shows like usual select query with no grouping.

Kindly need your help, thanks

>Solution :

We would generally handle your requirement from the presentation layer (e.g. PHP), but if you happen to be using MySQL 8+, here is a way to do this directly from MySQL:

SELECT
    CASE WHEN ROW_NUMBER() OVER (PARTITION BY wallet_id ORDER BY id) = 1
         THEN wallet_id END AS wallet_id,
    id,
    wallet_name
FROM wallet w
ORDER BY w.wallet_id, id;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading