0

I have a website with a database over different phones. And to gather data from the database, I have this code

//echo "SELECT * FROM `product_info` DESC";die;
    $filter = $this->queries->custom("SELECT * FROM `product_info`GROUP BY 'model' ORDER BY  `model` ASC");

When I use this code now, it only shows one model, instead of all the different models.

Since i've added some of the models more than once i Would like to sort those out.

Like, i have fx added the iPhone 7 5 different times in my database and of course i wouldn't want the site to show it 5 times, therefore i would like to sort after models and only show each model one time. I've read about the array_unique, but didn't quite understand it.

  • best way to do it is it by filtering the query. Add select distinct model from product_info. Simple, clear, efficent – Rafael Shkembi Nov 22 '16 at 14:53
  • How does your data in the table look? Is it possible that all of your phones have the same value for `model`? – simon Nov 22 '16 at 15:05
  • no, the value for model changes for each phone, that's why it's the best sorting option, if I could avoid duplicates. Model columns are filled with the model of the phones like: Galaxy S7, iPhone 7, iPhone SE, Honor 8 and so on.. – Mikkel Sønderskov Sørensen Nov 22 '16 at 15:12

2 Answers2

0

Just add the DISTINCT keyword

SELECT DISTINCT * FROM `product_info`GROUP BY 'model' ORDER BY  `model` ASC
0

I guess I found the problem, it's a little typo. You used single quotes ( ' ) instead of backticks ( ` ) for your GROUP BY column. Single quotes are for string values. This means that your current query groups by the string "model" (and not by the column model), which is of course the same string for every record.

So either use backticks:

SELECT * FROM `product_info` GROUP BY `model` ORDER BY `model` ASC

or omit the backticks at all because they are only necessary when your column is a reserved keyword:

SELECT * FROM product_info GROUP BY model ORDER BY model ASC
simon
  • 2,896
  • 1
  • 17
  • 22