Mysql
 sql >> Database >  >> RDS >> Mysql

Seleziona righe univoche, in base al valore in una colonna secondaria

Probabilmente hai bisogno di questo:

SELECT
  MIN(id) id,
  code,
  MIN(product_name) product_name,
  MIN(supplier) supplier
FROM
  yourtable
WHERE
  (code, CASE WHEN supplier='Supplier1' THEN '' ELSE supplier END)
  IN (SELECT   code, min(CASE WHEN supplier='Supplier1' THEN '' ELSE supplier END)
      FROM     yourtable
      GROUP BY code)
GROUP BY
  code
ORDER BY
  min(id);

Si prega di vedere violino qui .