Solved

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

Posted on 2011-03-14
4
374 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
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 40

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
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.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

770 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