**Update: Title should have read: Sql Query - Unique item with latest entry
Hi there,
I have a two tables. users and rsvp.
Users has user detail in it (id, name, email)
rsvp has rsvp status in it (answerid, userid, eventid, answer)
Note:
answer 1 = coming
answer 2 = not coming
answer 3 = maybe
answer 4 (pseudo answer) = no reply
Please note that "answer 4" never ever appears in the actual rsvp table data. It is constructed on the fly with the below SQL
A query to see whos coming, whos not, whos maybe coming AND who has not responded looks as follows:
SELECT
usrs.name,
usrs.email,
(SELECT CASE
WHEN rsvp.answer = 1 THEN 'Yes, see you there'
WHEN rsvp.answer = 2 THEN 'Cannot make it'
WHEN rsvp.answer = 4 THEN 'Cannot make it'
WHEN rsvp.answer = 3 THEN 'Maybe'
ELSE 'No Reply' END) as attendance,
(SELECT CASE
WHEN rsvp.answer = 1 THEN "1"
WHEN rsvp.answer = 2 THEN "3"
WHEN rsvp.answer = 4 THEN "4"
WHEN rsvp.answer = 3 THEN "2"
ELSE "5" END) as ordering
FROM jos_users usrs
LEFT JOIN jos_rsvp as rsvp on usrs.id = rsvp.userid
WHERE usrs.id NOT IN (62,63,128)
ORDER BY ordering
The same user may reply more than once. I.e. First they reply YES and then later they change their mind and reply NO.
rsvp answers are not updated, a new record is simply inserted for the new answer. The problem with the SQL above is that it does not take this duplication into account.
I'm trying to figure out how to change it so that only the last answer for each user is shown in the results. It seemed simple at first, but now I cant figure it out.
Using MySQL.
Please Help,
Losing Hair
n4~