Home > Net >  Selecting unique column values and returning corresponding values in mysql table
Selecting unique column values and returning corresponding values in mysql table

Time:12-10

SlNo UserID points
1 001 Three
2 002 Three
3 001 Three

I have the following table named 'userdata' and I would like to get the points of just the unique usernames.

SELECT points from userdata where USERID==distinct

Is there a functionality in mysql that works similar to == ?

CodePudding user response:

Use Distinct

Query

Select distinct UserId, points
From userdata;

CodePudding user response:

There are some possibilities

IN clause or a INNER JOIN

Latter will be faster on big tables

CREATE TABLE userdata (
  `SlNo` INTEGER,
  `UserID` INTEGER,
  `points` VARCHAR(5)
);

INSERT INTO userdata
  (`SlNo`, `UserID`, `points`)
VALUES
  ('1', '001', 'Three'),
  ('2', '002', 'Three'),
  ('3', '001', 'Thr');
SELECT `points` FROM userdata WHERE `UserID` IN (SELECT `UserID` FROM userdata GROUP BY `UserID` HAVING COUNT(*) = 1)
| points |
| :----- |
| Three  |
SELECT `points` 
FROM userdata  u1 INNER JOIN (SELECT `UserID` FROM userdata GROUP BY `UserID` HAVING COUNT(*) = 1) u2  ON u1.`UserID` = u2.`UserID`
| points |
| :----- |
| Three  |

db<>fiddle here

  • Related