-1

I have a MySQL table with id, username, datetime and entry_id. I'm logging access to each entry. But I need just one row with same username and entry_id, because I use it just for notifications. How could I programatically delete useless rows? (using PHP and/or SQL)

EDIT:

username is normal string, ON DUPLICATE KEY wasn't working, because I want to check for duplicity just combination of same username and entry_id. I need just one row, with specific entry_id and username. I want to update just datetime. It's hard to explain :)

So, on each visit is insered new row. I just want datetime to update in every unique username and entry_id combination and not to create always new row.

Thank you for advance!

david8
  • 444
  • 9
  • 23
  • What have you tried so far? Could you provide more details? Are you saying that a single user (id = 1, for example), should only have one entry (row), but you need to update datetime each time? – versalle88 May 27 '15 at 19:39
  • very ambigous information – Alex May 27 '15 at 19:41

2 Answers2

0

Just check for a previous entry in the database. If there is one, update it, if there is not an entry - insert one.

Karl
  • 833
  • 6
  • 13
  • 1
    https://stackoverflow.com/questions/1218905/how-do-i-update-if-exists-insert-if-not-aka-upsert-or-merge-in-mysql I think this is exactly what I want. Thank you – david8 May 27 '15 at 19:46
  • Then use a `SELECT FROM table WHERE username="username" AND entry_id="id"` if it exists you can just update it in a new query. If it doesn't exist create a new one. The link you provided doesn't fill the same function. – Karl May 27 '15 at 20:25
0

After reading your edit it sounds like you want something like:

SELECT username, entry_id FROM <table name> WHERE username = <username> AND entry_id = <entry_id>;

If you want to update the datetime column for that row:

UPDATE <table>  SET datetime = <yourdatetime> WHERE username = <username> AND entry_id = <entry_id>
MGrantley
  • 41
  • 4