Skip to content

MySQL

COUNT con condición sin utilizar un WHERE

SELECT sum(if(t.cost_premium_insurance > 0, 1, 0)) as viajesConSeguro
FROM TABLA_WAPA

Condición de columna en where

Dependiendo de algún campo, la condición del where puede depender de una columna u otra:

Solo meter un case en el where con la condición y seleccionando la columna

select *
from TABLAS_Y_JOINS
WHERE
(case
  when refund_id is null and gc.payment_id is not null then
    pa.created_at
  when refund_id is null and gc.payment_id is null then
    gc.created_at
  WHEN refund_id is not null then
    pa2.created_at
end
) >= '{start_date}'

Show processes and kill them

SHOW PROCESSLIST;

KILL 38239;
KILL 38240;

-- To make it more automatically
SELECT concat('KILL ',id,';') FROM information_schema.processlist;

-- And execute the output one by one

Match bits

Match bit position, useful when there is info by bit level (i.e user inactive reasons: bit 0 = reason x, bit 1 = reason y ...)

SELECT
  *
FROM
  User u
WHERE CONVERT(inactive_reason USING BINARY) & 1 << 17 > 0 -- check if bit at 17th position is 1