Hello, I'm dealing with sql and my sql is not as good as my ruby, so I hope you can help me out.
I have da table that looks like this:
messages:
id INTEGER PRIMARY KEY AUTO_INCREMENT
to VARCHAR(255)
text text
shown DATETIME
For now my script generates this part depending on the number of online players.
"to = 'STEAM_0:0:xxx' OR to = 'STEAM_0:0:xxy' OR to = 'STEAM_0:0:xxz'"
It's a listening of active players and I want to check weather they have some unread messages. Now with that string I can do and a sprintf with this :
SELECT * FROM messages WHERE shown IS NULL AND (%s)"
and get a nice formated string:
SELECT * FROM messages WHERE shown IS NULL AND (to = 'STEAM_0:0:xxx' OR to = 'STEAM_0:0:xxy' OR to = 'STEAM_0:0:xxz')
NOW I have just two problems:
- The sql returns more then 1 entry for every field entry
to
, I would like to return exactly one message for everyto
(LIMIT 1 byto
?) and it has to be the newest (first by id).
To make this clearer, let's assume I have a table like this:
id, to, text
1, "x", "text1"
2, "x", "text2"
3, "y", "text3"
4, "z", "text4"
5, "y", "text5"
6, "z", "text6"
7, "y", "text7"
I want to get the following:
1, "x", "text1"
3, "y", "text3"
4, "z", "text4"
- I would like to update the field
shown
within the same SQL call to NOW() for the retrieved entries.