2

This query doesn't work

SELECT * FROM Catalogue WHERE Catalogue.ID IN (
    SELECT ID_catalogue FROM Categories WHERE Categories.Product_type='xxx'
)

Error Code : 1064 You have an error in your SQL syntax near 'SELECT ID_catalogue FROM Categories WHERE Categories.Product_type='xxx' ) ' at line 2

Ok, that's because I am using a very old MySQL version.

What I am trying to get is

SELECT * FROM Catalogue WHERE Product_type='xxx' OR Catalogue.ID IN (
    SELECT ID_catalogue FROM Categories WHERE Categories.Product_type='xxx'
)

Is there any equivalent for that?

Thank you for all your comments.

2
  • 1
    What mysql version do you use? Seems like you have 4.0 one :-S Commented Dec 16, 2010 at 0:44
  • 4
    What version of MySQL are you running? Subquery support was added in 4.1, in 2003. Commented Dec 16, 2010 at 0:44

2 Answers 2

4

If you're using mysql version <= 4.0 - then it is the reason, since subqueries were added in 4.1

     SELECT c.*,
            g.ID_catalogue
       FROM Catalogue c
  LEFT JOIN Categories g ON g.ID_catalogue = c.ID
                        AND g.Product_type='xxx'
     HAVING ID_catalogue IS NOT NULL
         OR Product_type = 'xxx'

You should add composite index ID_catalogue + Product_type for table Categories AND index Product_type for table Catalogue to have this query performed fast

Sign up to request clarification or add additional context in comments.

7 Comments

Is there any equivalent query for version 3.23?
@aximili: the latest 3.23 release was in September 11, 2003. I'm strongly recommend you to update it :-S
You have to rewrite this query as a join. It is fairly straightforward. But you should really take zerkms's advice and upgrade this server. You're a decade behind the times.
lol thanks for the release date, nice to know! =p Sorry my query actually has an OR in it, see my updated question. Thanks zerkms and Dan!
I have tried that as well as EXISTS, they don't seem to work either +_+ (You have an error in your SQL syntax near 'UNION SELECT c.*... at line 5)
|
3

There is nothing wrong with your query. It is the version of MySQL that is more than likely your problem.

4 Comments

As has already been covered in the comments. Don't steal zerkms's reputation.
@dan Zerkms should have put it in an answer. Also see the other response. You should chide that person also for being a thief. I was not aware there was anything wrong with learning from the other comments but having the courage to put it in an answer, right or wrong.
@johnny: i don't like to guess. I'm answering only when I'm sure it is correct answer. In this particular case it can also be some kind of non-typed character right before select subquery.
@zerkms I was not saying you had to. I was responding more to dan not you. Sorry if you took offense. There have been times I should have put an answer instead of a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.