select distinct data from my table with mysql distinct function

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
LVL 1
BRMarketingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dorababu MSenior Software EngineerCommented:
You can also do as follows

SELECT * FROM table  where uid = 1 ORDER BY id DESC LIMIT 1;
BRMarketingAuthor 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 EngineerCommented:
Yes it do
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Dorababu MSenior Software EngineerCommented:
But it will return multiple rows for the same user in descending order
BRMarketingAuthor Commented:
thank you,
how can I select the users and the last entered answer distinctly?
Dorababu MSenior Software EngineerCommented:
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;
BRMarketingAuthor Commented:
I need all records
Dorababu MSenior Software EngineerCommented:
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

Dorababu MSenior Software EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BRMarketingAuthor Commented:
this is a perfect answer. thank you
Dorababu MSenior Software EngineerCommented:
You are welcome :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.