View Single Post

  #2 (permalink)  
Old 04-17-2007
Captain Paralytic
 
Posts: n/a
Default Re: Problems with a subquery

On Apr 17, 2:46 pm, "f.mardini" <f.mard...@gmail.com> wrote:
> Hi,
>
> I have this query that works coorrectly
>
> ( SELECT DISTINCT t1.connected_id FROM connections as t1 WHERE
> ( t1.connectee_id = 1 ) ) UNION ( SELECT DISTINCT t2.connected_id FROM
> connections as t1, connections as t2 WHERE ( t1.connectee_id = 1 ) AND
> ( t1.connected_id = t2.connectee_id ) )
>
> but when i use it as a subquery like this
>
> SELECT * FROM users WHERE id IN ( ( SELECT DISTINCT t1.connected_id
> FROM connections as t1 WHERE ( t1.connectee_id = 1 ) ) UNION ( SELECT
> DISTINCT t2.connected_id FROM connections as t1, connections as t2
> WHERE ( t1.connectee_id = 1 ) AND ( t1.connected_id =
> t2.connectee_id ) ) )
>
> i get a syntax error
> ERROR 1064 (42000): 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 ( SELECT DISTINCT t2.connected_id FROM
> connections as t1, connections as t' at line 1
>
> I don't understand what is the problem. How can i get this to work?
> Thanks
> f.mardini


Use LEFT JOINs instead of subqueries, they are sooooo much mrore
efficient.

Reply With Quote