Solved

Need help with ORDER BY on this MySQL many to many and UNION

Posted on 2011-03-14
4
387 Views
Last Modified: 2012-06-21
Have been getting some help with the following query.

At the moment the query makes the union of the two tables and then ORDERs the result.

I would like to have that changed so that the two tables are ORDERED by projects.project_position BEFORE the UNIOIN occurs.

That way each table can be ORDERED separately rather than as one, at the end. I'm just not sure how to add the two ORDER BY to the query.


SELECT * FROM (SELECT projects.project_short_name, projects.project_synopsis, projects.project_position, projects.project_name 
FROM projects 

INNER JOIN p_c ON p_c.project_id = projects.project_id 
INNER JOIN c ON c.category_id = p_c.category_id 

WHERE ( c.category_name = 'Type-A' OR c.category_name = 'Type-B' OR c.category_name = 'Type-C' OR c.category_name = 'Type-D' ) 
AND NOT projects.project_visibility =0 

UNION SELECT concat( projects.project_short_name, '_grey' ), projects.project_synopsis, projects.project_position, projects.project_name 
FROM projects 

INNER JOIN p_c ON p_c.project_id = projects.project_id 
INNER JOIN c ON c.category_id = p_c.category_id 
WHERE NOT c.category_name = 'Type-A' AND NOT c.category_name = 'Type-B' AND NOT c.category_name = 'Type-C' AND NOT c.category_name = 'Type-D' 
AND NOT projects.project_visibility =0 
AND projects.project_id NOT IN 
( SELECT DISTINCT projects.project_id FROM projects 

INNER JOIN p_c ON p_c.project_id = projects.project_id 
INNER JOIN c ON c.category_id = p_c.category_id 
WHERE c.category_name = 'Type-A' OR c.category_name = 'Type-B' OR c.category_name = 'Type-C' OR c.category_name = 'Type-D' )
) 
AS t1 ORDER BY cast(project_position as signed integer)

Open in new window

0
Comment
Question by:sany101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 20

Expert Comment

by:Mark Brady
ID: 35134136
Have you tried

SELECT * FROM (SELECT projects.project_short_name, projects.project_synopsis, projects.project_position, projects.project_name
FROM projects ORDER BY `projects.project_position` ASC

INNER JOIN p_c ON p_c.project_id = projects.project_id
INNER JOIN c ON c.category_id = p_c.category_id
0
 
LVL 18

Expert Comment

by:Matthew Kelly
ID: 35134164
One way to accomplish this is to add a fake column to each union query, and then add it to the ORDER BY.

So for example, below I add a column called 'SortBy' to each query and then sorts by that value first. As such the first table, since it has a '1' for SortBy will always be sorted on top, and then the second table will always be on the bottom.
0
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 35134220
You have two different data sets. Assume data_set_1 and data_set_2. When you do UNION between these two data sets, it eliminates all duplicate records and give you one  record for the combination of columns mentioned in the SELECT clause. Now you have to apply the ORDER BY on the whole set after doing the UNION operation to get the result in proper order.
If you apply ORDER BY individually, then it will display the records in the order of first data set and then next data set. However if you are interested in that, you can try this.
SELECT * 
  FROM (SELECT projects.project_short_name, 
               projects.project_synopsis, 
               projects.project_position, 
               projects.project_name 
          FROM projects 
               INNER JOIN p_c 
                 ON p_c.project_id = projects.project_id 
               INNER JOIN c 
                 ON c.category_id = p_c.category_id 
         WHERE ( c.category_name = 'Type-A' 
                  OR c.category_name = 'Type-B' 
                  OR c.category_name = 'Type-C' 
                  OR c.category_name = 'Type-D' ) 
               AND NOT projects.project_visibility = 0 
         ORDER BY CAST(project_position AS SIGNED INTEGER)) AS t1 
UNION 
SELECT * 
  FROM (SELECT CONCAT(projects.project_short_name, '_grey'), 
               projects.project_synopsis, 
               projects.project_position, 
               projects.project_name 
          FROM projects 
               INNER JOIN p_c 
                 ON p_c.project_id = projects.project_id 
               INNER JOIN c 
                 ON c.category_id = p_c.category_id 
         WHERE NOT c.category_name = 'Type-A' 
               AND NOT c.category_name = 'Type-B' 
               AND NOT c.category_name = 'Type-C' 
               AND NOT c.category_name = 'Type-D' 
               AND NOT projects.project_visibility = 0 
               AND projects.project_id NOT IN 
                   (SELECT DISTINCT projects.project_id 
                      FROM projects 
                           INNER JOIN p_c 
                             ON p_c.project_id = 
                                projects.project_id 
                           INNER JOIN c 
                             ON c.category_id = 
                                p_c.category_id 
                     WHERE c.category_name = 'Type-A' 
                            OR c.category_name = 'Type-B' 
                            OR c.category_name = 'Type-C' 
                            OR c.category_name = 'Type-D') 
         ORDER BY CAST(project_position AS SIGNED INTEGER)) AS t2  

Open in new window

0
 

Author Closing Comment

by:sany101
ID: 35134486
That's perfect and exactly waht I was after thanks again.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question