473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Trying to UNION three queries to one, not working, getting error !

67
Hi friends, I'm having three queries that works perfectly ...

Expand|Select|Wrap|Line Numbers
  1. SELECT ib.id as id, ib.titre as title, ib.date_expire as date_fin, ib.created_at as date_creation, eb.content as content, ''
  2. FROM tbl_announce AS ib
  3. LEFT JOIN tbl_announce_content AS eb ON ib.id = eb.rap_id
  4. WHERE ib.lang = 'fr'
  5. AND (
  6. ib.date_expire = '0000-00-00'
  7. OR ib.date_expire >= '2008-02-01'
  8. )
  9. AND ib.publish =1
  10. GROUP BY ib.id
  11. ORDER BY ib.date_publish DESC
Expand|Select|Wrap|Line Numbers
  1. (
  2. SELECT id, title, date_fin, date_creation, content, 0 AS sort_by
  3. FROM events WHERE date_fin >= '2008-02-01'
  4. AND lang = 'fr'
  5. AND publish =1
  6. )
  7. UNION (
  8. SELECT id, title, date_fin, date_creation, content, 1 AS sort_by
  9. FROM events WHERE date_fin = '0000-00-00'
  10. AND lang = 'fr'
  11. AND publish =1
  12. )
  13. ORDER BY sort_by, date_fin ASC , date_creation ASC
Expand|Select|Wrap|Line Numbers
  1. (
  2. SELECT id, title, date_fin, date_creation, content, 0 AS sort_by
  3. FROM job
  4. WHERE date_fin >= '2008-02-01'
  5. AND lang = 'fr'
  6. AND publish =1
  7. )
  8. UNION (
  9. SELECT id, title, date_fin, date_creation, content, 1 AS sort_by
  10. FROM job
  11. WHERE date_fin =0000 -00 -00
  12. AND lang = 'fr'
  13. AND publish =1
  14. )
  15. ORDER BY sort_by, date_fin ASC , date_creation ASC
I wanted to make one query from these three ... so I tried an UNION ALL

Expand|Select|Wrap|Line Numbers
  1. (
  2. SELECT ib.id as id, ib.titre as title, ib.date_expire as date_fin, ib.created_at as date_creation, eb.content as content, ''
  3. FROM tbl_announce AS ib
  4. LEFT JOIN tbl_announce_content AS eb ON ib.id = eb.rap_id
  5. WHERE ib.lang = 'fr'
  6. AND (
  7. ib.date_expire = '0000-00-00'
  8. OR ib.date_expire >= '2008-02-01'
  9. )
  10. AND ib.publish =1
  11. GROUP BY ib.id
  12. ORDER BY ib.date_publish DESC
  13.  
  14. ) UNION ALL (
  15.  
  16. (
  17. SELECT id, title, date_fin, date_creation, content, 0 AS sort_by
  18. FROM events WHERE date_fin >= '2008-02-01'
  19. AND lang = 'fr'
  20. AND publish =1
  21. )
  22. UNION (
  23. SELECT id, title, date_fin, date_creation, content, 1 AS sort_by
  24. FROM events WHERE date_fin = '0000-00-00'
  25. AND lang = 'fr'
  26. AND publish =1
  27. )
  28. ORDER BY sort_by, date_fin ASC , date_creation ASC
  29.  
  30. ) UNION ALL (
  31.  
  32. (
  33. SELECT id, title, date_fin, date_creation, content, 0 AS sort_by
  34. FROM job
  35. WHERE date_fin >= '2008-02-01'
  36. AND lang = 'fr'
  37. AND publish =1
  38. )
  39. UNION (
  40. SELECT id, title, date_fin, date_creation, content, 1 AS sort_by
  41. FROM job
  42. WHERE date_fin =0000 -00 -00
  43. AND lang = 'fr'
  44. AND publish =1
  45. )
  46. ORDER BY sort_by, date_fin ASC , date_creation ASC)
But this is not working ... :(

Getting error -

Expand|Select|Wrap|Line Numbers
  1. #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION (
  2. SELECT id, title, date_fin, date_creation, content, 1 AS sort_by
  3. FROM ' at line 22
Wondering, can anyone help to figure out what's going wrong here ... ?
Feb 1 '08 #1
1 2167
kigoobe
67
Ho, this issue is solved, thanks to Rudy. Here is the solution -

Expand|Select|Wrap|Line Numbers
  1. ( SELECT ib.id                           
  2.        , ib.titre as title               
  3.        , ib.date_expire as date_fin      
  4.        , ib.created_at as date_creation  
  5.        , eb.content as content           
  6.        , 0 as sort_by                    
  7.     FROM tbl_announce AS ib              
  8.   LEFT                                   
  9.     JOIN tbl_announce_content AS eb      
  10.       ON eb.rap_id = ib.id               
  11.    WHERE ib.lang = 'fr'                  
  12.      AND ( ib.date_expire = '0000-00-00' 
  13.         OR ib.date_expire >= '2008-02-01'
  14.          )                               
  15.      AND ib.publish =1 )
  16. UNION ALL
  17. ( SELECT id, title, date_fin, date_creation, content             
  18.        , CASE WHEN date_fin = '0000-00-00' THEN 1                
  19.                                            ELSE 0 END  AS sort_by
  20.     FROM events                                                  
  21.    WHERE ( date_fin = '0000-00-00'                               
  22.         OR date_fin >= '2008-02-01'                              
  23.          )                                                       
  24.      AND lang = 'fr'                                             
  25.      AND publish = 1 )
  26. UNION ALL
  27. ( SELECT id, title, date_fin, date_creation, content                                           
  28.        , CASE WHEN date_fin = '0000-00-00' THEN 1                
  29.                                            ELSE 0 END  AS sort_by
  30.     FROM job                                                  
  31.    WHERE ( date_fin = '0000-00-00'                               
  32.         OR date_fin >= '2008-02-01'                              
  33.          )                                                       
  34.      AND lang = 'fr'                                             
  35.      AND publish = 1 )
  36. ORDER 
  37.     BY sort_by
  38.      , date_fin
  39.      , date_creation
Feb 3 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: lyn.duong | last post by:
Hi, I have a large table (about 50G) which stores data for over 7 years. I decided to split this table up into a yearly basis and in order to allow minimum changes to the applications which...
6
by: _link98 | last post by:
Problem: getting SQL0181N for queries on nicknames to remote Union-All-View. Can't see what I'm doing wrong yet, maybe someone has seen this before. Environment: UDB ESE 8.1 + FIXPAK 9A, on...
0
by: s_gregory | last post by:
The mdb is considerable size 70 +- mb. A complex union query was working well, but when an additional union select... was added into the query, selecting identical fields from a different source,...
4
by: Missy | last post by:
We’ve recently upgraded our computer system to XP. My union query (which was working perfectly for years) now returns hieroglyphics instead of invoice numbers. When I run the 2 queries...
12
by: Susan Bricker | last post by:
For those of you who have been following my posts - they all pertain to a Dog Competition Organization's Database. There are three classes that the dogs can participate: NOVICE, OPEN, and...
3
by: mikes | last post by:
I have 2 separate queries, which effectively are the same except they draw data from separate tables. Both tables are (design-wise) identical, only the data is different. for each query, there are...
4
by: spam | last post by:
If I run the following query in Access 2002 then I get the expected result: SELECT * FROM CSVImport UNION SELECT * FROM AssetTemp; I get the contents of both tables with no duplicates. If I...
21
by: Killer42 | last post by:
Hi all. As has been mentioned here recently, I have a front-end database which just includes some queries and a bunch of (rather large) linked tables. For reasons of (very poor) performance, I had...
1
by: rileyjane | last post by:
Hello, I am running a Union query in Access 2003 that combines two other queries. When I run each query individually, they open fine with no problems. But when I try to run the Union query, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.