consult a single distinct with condition

2

I have a table with 4 columns

id, descripcion_articulo, cantidad, modelo, marca

and this query:

("select distinct descripcion_articulo from " . TABLE_ENTREGAS . "
where numeracion = '' order by descripcion_articulo ASC")

to generate this query are just the articles that I want to show, this is perfect, but when adding the other columns that are missing the query:

("select distinct id, descripcion_articulo, cantidad, modelo, marca 
from " . TABLE_ENTREGAS . "
where numeracion = '' order by descripcion_articulo ASC")

shows more values than I wish, in fact it repeats values of article_description, that I do not want, I only want a single unique value of article_description

I need to add the other elements id , cantidad , modelo , marca to the query made. how to make the query.

and add an additional column that adds all the quantities it contains = article_description

    
asked by Ivan Diaz Perez 31.07.2018 в 15:28
source

1 answer

1

Group by the field you have used for the distinct and use this same field to count. The query will work better if this field is an index, I have set the example with the description field that you use at the beginning, depending on the logic that your query and your data follow and the content may be better to use another field.

"SELECT COUNT(descripcion_articulo) AS total, 
  id, descripcion_articulo, cantidad, modelo, marca from " . TABLE_ENTREGAS . "
WHERE numeracion = '' 
GROUP BY descripcion_articulo
ORDER BY descripcion_articulo ASC"
    
answered by 31.07.2018 / 16:03
source