select distinct data from my table with mysql distinct function

BR
BR used Ask the Experts™
on
Dear Experts, I use PHP and mysql.

I'd like to select distinct voters from my table. Some of my users have voted more than once.

if user voted more than once, I'd like the get the latest answer.

how can I select distinct uid and answer column from my table with the latest vote?

how can I select the later vote? I want to select the last insert.

(I have an autoincrement id column on my table. )

I wrote it like this? is this correct?

SELECT DISTINCT uid,answer,city FROM mytable order by id DESC
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dorababu MSenior Software Engineer
Distinguished Expert 2018

Commented:
You can also do as follows

SELECT * FROM table  where uid = 1 ORDER BY id DESC LIMIT 1;
BRDigital Marketing

Author

Commented:
Dear  Dorababu M,
is my sentence correct?

with this sentence, do I get the latest inserts?

SELECT DISTINCT uid,answer,city FROM mytable order by id DESC
Dorababu MSenior Software Engineer
Distinguished Expert 2018

Commented:
Yes it do
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Dorababu MSenior Software Engineer
Distinguished Expert 2018

Commented:
But it will return multiple rows for the same user in descending order
BRDigital Marketing

Author

Commented:
thank you,
how can I select the users and the last entered answer distinctly?
Dorababu MSenior Software Engineer
Distinguished Expert 2018

Commented:
As said you can use this if you want to filter by userid, do you need all the records?

SELECT * FROM table  where uid = 1 ORDER BY id DESC LIMIT 1;
BRDigital Marketing

Author

Commented:
I need all records
Dorababu MSenior Software Engineer
Distinguished Expert 2018

Commented:
Ok try this if you have a date column

select col1,col2, max(yourdate) as date
from tbl 
group by uid;

Open in new window

Senior Software Engineer
Distinguished Expert 2018
Commented:
Sample fiddle here with sample data

CREATE TABLE tbl
	(`ID` int, `USERNAME` varchar(5), `COURSE_ID` int, `LDATE` varchar(10))
;
	
INSERT INTO tbl
	(`ID`, `USERNAME`, `COURSE_ID`, `LDATE`)
VALUES
	(1, 'user1', 22, '2013-06-01'),
	(2, 'user1', 54, '2013-06-03'),
	(3, 'user1', 22, '2013-06-03'),
	(4, 'user2', 71, '2013-06-04')
;

select username, course_id, max(ldate) as date
from tbl 
where username='user1'
group by course_id;


select username, course_id, max(ldate) as date
from tbl 
group by username, course_id;

Open in new window


http://sqlfiddle.com/#!9/7e188/1
BRDigital Marketing

Author

Commented:
this is a perfect answer. thank you
Dorababu MSenior Software Engineer
Distinguished Expert 2018

Commented:
You are welcome :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial