Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-14
4
Medium Priority
?
390 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 41

Accepted Solution

by:
Sharath earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

916 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