Request for updating by number of records



  • There's a request for updating:

    Mysqli_query. users SET name WHERE name - 'Everything' LIMIT 10'

    His logic can be said as follows:
    We find all users with a name. васяand we're gonna fix the name a little. But we only send it to ten users.

    I'd like to know how to make sure that when we find the records, if there's 10 "vass" or more, then we're updating the tens." But if there's less than 10 in "all" database (e.g. 9), we don't update anyone.

    In other words: We need to find and update a minimum of 10 records, if that's the number of records in the database, and if the records are smaller, we don't do anything. If there's more records than 10, we're still updating 'cause the right amount is on.



  • Try that.

    UPDATE users u1
    INNER JOIN ( SELECT count(*) as cou 
                   FROM users as u2 
                  WHERE u2.name = 'вася') j1
       SET u1.name = 'Василий' 
     WHERE u1.name = 'вася' 
       AND j1.cou > 10
     LIMIT 10
    

    It works so (1).

    SELECT count(*) as cou 
      FROM users as u2 
     WHERE u2.name = 'вася'
    

    determines the number of records from 'ware'

    (2) This is an update if cou > 10 (numbers 10) and LIMIT 10 (only 10 of them)

       SET u1.name = 'Василий' 
     WHERE u1.name = 'вася' 
       AND j1.cou > 10
     LIMIT 10
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2