dierre
dierre

Reputation: 7210

Using limit in a left join in mysql

Guys can you help me with this not working query?

UPDATE DB_1 
left join blacklist as blk 
    on DB_1.last_email=blk.email 
SET DB_1.sampling = ? 
WHERE blk.email IS NULL 
    and DB_1.sampling IS NULL 
LIMIT "+slot;

I need to update 25k row at the time but doing this is not working. How can I create a query that does work? I was thinking to drop the left join and use an IN for the join.

Upvotes: 0

Views: 176

Answers (1)

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115550

Do you you want to update the rows in DB_1 that are not in the blacklist?

You can try:

UPDATE 
    DB_1 
SET 
    DB_1.sampling = ? 
WHERE 
    NOT EXISTS
      ( SELECT *
        FROM blacklist AS blk 
        WHERE blk.email = DB_1.last_email
      ) 
ORDER BY  
    <something>
LIMIT 
    <whatever>

or:

UPDATE 
        DB_1 AS upd
    JOIN
        ( SELECT t.PK
          FROM 
                  DB_1 AS t
              LEFT JOIN 
                  blacklist AS blk 
                      ON blk.email = t.last_email
          WHERE 
              blk.email IS NULL
          ORDER BY  
              <something>
          LIMIT 
              <whatever>
       ) AS lim
           ON lim.PK = upd.PK
SET 
    upd.sampling = ? 

Upvotes: 1

Related Questions