问题
I am trying to use a UNION statement in an old BugZilla MySQL DB running MySQL 3.23. Here is a simplified version of what I am trying to do. It's really nothing exotic but MySQL keeps saying that something is wrong.
(select bug_id,rep_platform from bugs where rep_platform='XX')
UNION
(select bug_id,rep_platform from bugs where rep_platform='YY');
The result I get is:
ERROR 1064 (HY000): You have an error in your SQL syntax near '(select bug_id,rep_platform from bugs where rep_platform='XX')
UNION
(select bu' at line 1
This is really about as simple of a union statement as you can get. Any ideas?
EDIT: I ran a very similar query on a MySQL 5 DB and it worked just fine. Is these some special UNION syntax on MySQL 3 that doesn't show up in documentation or Google searches?
回答1:
The reason why the UNION is not working in MySQL 3 is because MySQL 3 does not support UNION statements :) Take a look at the official documentation.
UNION is used to combine the result from multiple SELECT statements into a single result set. UNION is available from MySQL 4.0.0 on.
Take a look at this article for workarounds.
回答2:
Why not just do this instead:
SELECT DISTINCT bug_id, rep_platform
FROM bugs
WHERE rep_platform IN ('XX', 'YY');
回答3:
I think you meant
select bug_id,rep_platform from bugs A where rep_platform='XX'
UNION
select bug_id,rep_platform from bugs where rep_platform='YY';
Joe's answer is much easier and pleasing on the eyes. +1 for Joe.
The two changes I made are
- I dropped all parentheses
- I put a table alias A next to bugs in the first SELECT
来源:https://stackoverflow.com/questions/9202667/mysql-3-23-union-fails-with-error-1064