@
@
@atambalasi2016-02-24 18:15:12
PHP
@atambalasi, 2016-02-24 18:15:12

How to delete the first user entry?

There is a table with links
id user_id link
If the number of records of one user is more than 10, then delete the 1st added record and insert the 10th new record.
Something like this we get the number of records
SELECT COUNT(*) FROM links WHERE user_id=43
Check in php

if($count <= 5)
{
   //sql insert
}else{
   //sql insert
  $links_id_arr = ['3', '14', '35', '10', '43' , '23'];
  //Находим самый маленкий ИД в $links_id_arr
  $min_in_arr  = 3;
$sql = "DELETE FROM links WHERE user_id=43 AND id= $min_in_arr";
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor, 2016-02-24
_

if it’s stupid in the forehead, then the
first query is count and delete with a lower id if count = 10
the second is inserted
if it’s smart, then the field with the date of creation / update of the record would not interfere and then everything can be done in one query with if
, well, and you are really sure that it is necessary delete?

N
nozzy, 2016-02-24
@nozzy

delete from links 
where id = (
  select min(id) 
  from links
  group by user_id
  having count(id) > 9
  and user_id = 222
);
insert into ..... ;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question