A
A
alestro2016-02-01 15:24:04
MySQL
alestro, 2016-02-01 15:24:04

How to replace duplicate value with null in sql select?

There are two tables between which a many-to-many relationship is established using a join table.

select f.name as filter_name,pg.name as group_name from filters as f join filters_products_group as fpg on f.id = fpg.filters_id join products_group as pg on fpg.products_group_id = pg.id

With this request, I pull out the fields I need, but I would like the filter_name field to be null if the value is repeated. How can I edit a request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Immortal_pony, 2016-02-01
@Immortal_pony

SELECT 
  CASE 
    WHEN (GROUP_CONCAT(f.name SEPARATOR '|||') LIKE '%|||%') THEN
      NULL
    ELSE
      f.name 
  END AS filter_name,
  pg.name AS group_name 
FROM 
  filters AS f 
  JOIN filters_products_group AS fpg ON f.id = fpg.filters_id 
  JOIN products_group AS pg ON fpg.products_group_id = pg.id
GROUP BY f.name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question