简体   繁体   中英

MySQL Query with DISTINCT and ORDER BY

Here is my query:

$result = $mysqli->query('SELECT DISTINCT SKU_SIZE_PART1 
                          FROM SKU_DATA 
                          ORDER BY SKU_SIZE_PART1 DESC');

Now this works perfect for SKU_SIZE_PART1 but I have 2 more parts that I need to grab. Now when I put a comma and do this: 'SKU_SIZE_PART1, SKU_SIZE_PART2, SKU_SIZE_PART3' then the DISTINCT doesn't work and I get a ton of duplicates, and then I'm not sure how to order the query so that all of them are ordered by the size and DESC.

Does that make sense? I could just duplicate that query 2 more times and have 3 separate queries but I would like to know how to accomplish this with just one.

I'm not positive that I understand what you're trying to do, but it sounds like you might actually want something like this:

SELECT SKU_SIZE_PART1 AS SKU_SIZE_PART
  FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART2 AS SKU_SIZE_PART
  FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART3 AS SKU_SIZE_PART
  FROM SKU_DATA
 ORDER BY SKU_SIZE_PART DESC

which will return all distinct SKU_SIZE_PART1/2/3 values in a single column, rather than all distinct (SKU_SIZE_PART1, SKU_SIZE_PART2, SKU_SIZE_PART3) triads in three columns.

After reading your question several times, I figured this might be what you are looking for:

SELECT SKU_SIZE_PART1 AS ssp
FROM SKU_DATA 
UNION
SELECT SKU_SIZE_PART2 AS ssp
FROM SKU_DATA 
UNION
SELECT SKU_SIZE_PART3 AS ssp
FROM SKU_DATA 
ORDER BY ssp DESC
SELECT d.sku_size_part1, d.sku_size_part2, d.sku_size_part3 
FROM sku_data d
WHERE d.id IN (
  SELECT s.id   <<--- replace `id` with the real primary-key for table `sku_data`
  FROM sku_data s 
  GROUP BY s.sku_size_part1)
ORDER BY d.sku_size_part1 DESC

Note that this will select rows more or less at random.
Although all sku_size_parts will be from the same row, lots of values will be hidden.
If you want to make the query stable, you need to add a having clause in the inner subselect.

Something like this:

SELECT d.sku_size_part1, d.sku_size_part2, d.sku_size_part3 
FROM sku_data d
WHERE d.id IN (
  SELECT s.id   <<--- replace `id` with the real primary-key for table `sku_data`
  FROM sku_data s 
  GROUP BY s.sku_size_part1
  HAVING s.sku_size_part2 = MIN(s.sku_size_part2) 
     AND s.sku_size_part3 = MIN(s.sku_size_part3))
ORDER BY d.sku_size_part1 DESC

Either that or you want @bfavaretto's UNION variant.

DISTINCT selects a distinct set of rows, not columns... the assumption/problem here is how to condense multiple columns. If you had the following table

  sku1 | sku2 | sku3
 ---------------------
    a  |   a  |  b
    b  |   b  |  b

Telling it to select destinct would return both rows because none of them are distinct, you couldn't just remove the third column because then the row data would be inconsistent. If you want everything in one table you can do this with subqueries.

 SELECT (SELECT DISTINCT SKU_SIZE_PART1 FROM SKU_DATA ORDER BY SKU_SIZE_PART1 DESC)
 as part1, (SELECT DISTINCT SKU_SIZE_PART2 FROM SKU_DATA ORDER BY SKU_SIZE_PART2 DESC)      
 as part2, (SELECT DISTINCT SKU_SIZE_PART3 FROM SKU_DATA ORDER BY SKU_SIZE_PART1 DESC)  
 as part3 FROM SKU_DATA

You can read up a little on how DISTINCT works to see why you can't just do SELECT DISTINCT SKU_SIZE_PART1, PART2, PART3. Somewhere like This Link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM