I saw similar questions here, but none of them helped me.
I have a table allowing to say Test (using mysql MyISAM)
The test has the following scheme:
TID INT PRIMARY KEY AUTO_INCREMENT TData varchar (data that i want to select) TUserID INT (this will be joined with users table, same users can have many records here) TViewedAt DATETIME (records each time user visits page, datetime format)
Now every time the user visits the test page at present, she adds entries to this table, recording the user ID, whoever visits, some data and the date and time the user visits, when the TID automatically increases each time.
Then I have a backend management page where I can see which user visited the test page, how many times, data and when. This is basically a table with a list of the last 20-25 rows. My query should select only one record per data. The data may be the same for the user, but at different times, but I only want to select one of the data for each user, which is the most recent. Thus, if a user visits the test page 10 times, 10 records are sent to the database, but only 1 last record in the table is displayed. If another user visits the page 15 times, he displays 1 record for this second user, which is the last, so in general we now have 2 rows displayed in the table. I got everything to work, but GROUP BY for some reason, and MAX (date) does not give me the latest datetime for each date.
here is my request:
SELECT * FROM Test WHERE TUserID = 123 GROUP BY TData ORDER BY TViewedAt
returns 2 rows, of which none of these lines is the last.
thanks alot