Can you spot the SQL syntax error?

View: New views
4 Messages — Rating Filter:   Alert me  

Can you spot the SQL syntax error?

by LAB-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


In my controller neighborMatches I have an array of ids:

neighborMatches = neighborMatches.map!{|n| n.id}

I want to find all the properties that match some conditions, and is
also in this list of ids:

@properties = Property.find(:all, :conditions => ["(NO_BEDROOMS > ?
AND NO_FULL_BATHS > ? AND SQUARE_FEET > ? AND LIST_PRICE > ? AND
LIST_PRICE < ? AND id IN ?)", beds, baths, sqft, priceLow, priceHigh,
neighborMatches]

I get an invalid SQL error: Mysql::Error: #42000You have an error in
your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near
'3,33,41,187,432,497,533,537,558,561,564,245,274,365,474,534,536,577,4,18,235,346'
at line 1: SELECT * FROM properties WHERE ((NO_BEDROOMS > '1' AND
NO_FULL_BATHS > '1' AND SQUARE_FEET > '500' AND LIST_PRICE > '0' AND
LIST_PRICE < '99999999' AND id IN
3,33,41,187,432,497,533,537,558,561,564,245,274,365,474,534,536,577,4,18,235,346,396,439,509,66,468))

Can anyone spot the SQL syntax error?  Thanks in advance!!

LAB


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Can you spot the SQL syntax error?

by Tiago Macedo-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I believe the IN clause needs parentheses:

AND id IN
(3,33,41,187)


Tiago Macedo

LAB wrote:

> In my controller neighborMatches I have an array of ids:
>
> neighborMatches = neighborMatches.map!{|n| n.id}
>
> I want to find all the properties that match some conditions, and is
> also in this list of ids:
>
> @properties = Property.find(:all, :conditions => ["(NO_BEDROOMS > ?
> AND NO_FULL_BATHS > ? AND SQUARE_FEET > ? AND LIST_PRICE > ? AND
> LIST_PRICE < ? AND id IN ?)", beds, baths, sqft, priceLow, priceHigh,
> neighborMatches]
>
> I get an invalid SQL error: Mysql::Error: #42000You have an error in
> your SQL syntax; check the manual that corresponds to your MySQL
> server version for the right syntax to use near
> '3,33,41,187,432,497,533,537,558,561,564,245,274,365,474,534,536,577,4,18,235,346'
> at line 1: SELECT * FROM properties WHERE ((NO_BEDROOMS > '1' AND
> NO_FULL_BATHS > '1' AND SQUARE_FEET > '500' AND LIST_PRICE > '0' AND
> LIST_PRICE < '99999999' AND id IN
> 3,33,41,187,432,497,533,537,558,561,564,245,274,365,474,534,536,577,4,18,235,346,396,439,509,66,468))
>
> Can anyone spot the SQL syntax error?  Thanks in advance!!
>
> LAB
>
>
> >
>  

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Can you spot the SQL syntax error?

by jesse_c :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


IN (value1/,/value2,..)



LAB wrote:

> In my controller neighborMatches I have an array of ids:
>
> neighborMatches = neighborMatches.map!{|n| n.id}
>
> I want to find all the properties that match some conditions, and is
> also in this list of ids:
>
> @properties = Property.find(:all, :conditions => ["(NO_BEDROOMS > ?
> AND NO_FULL_BATHS > ? AND SQUARE_FEET > ? AND LIST_PRICE > ? AND
> LIST_PRICE < ? AND id IN ?)", beds, baths, sqft, priceLow, priceHigh,
> neighborMatches]
>
> I get an invalid SQL error: Mysql::Error: #42000You have an error in
> your SQL syntax; check the manual that corresponds to your MySQL
> server version for the right syntax to use near
> '3,33,41,187,432,497,533,537,558,561,564,245,274,365,474,534,536,577,4,18,235,346'
> at line 1: SELECT * FROM properties WHERE ((NO_BEDROOMS > '1' AND
> NO_FULL_BATHS > '1' AND SQUARE_FEET > '500' AND LIST_PRICE > '0' AND
> LIST_PRICE < '99999999' AND id IN
> 3,33,41,187,432,497,533,537,558,561,564,245,274,365,474,534,536,577,4,18,235,346,396,439,509,66,468))
>
> Can anyone spot the SQL syntax error?  Thanks in advance!!
>
> LAB
>
>
> >
>  

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Can you spot the SQL syntax error?

by LAB-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


cool ... that was it!!  Thank you!!

...AND id IN (?)

On Aug 30, 4:49 pm, Jesse Clark <je...@...> wrote:

> IN (value1/,/value2,..)
>
> LAB wrote:
> > In my controller neighborMatches I have an array of ids:
>
> > neighborMatches = neighborMatches.map!{|n| n.id}
>
> > I want to find all the properties that match some conditions, and is
> > also in this list of ids:
>
> > @properties = Property.find(:all, :conditions => ["(NO_BEDROOMS > ?
> > AND NO_FULL_BATHS > ? AND SQUARE_FEET > ? AND LIST_PRICE > ? AND
> > LIST_PRICE < ? AND id IN ?)", beds, baths, sqft, priceLow, priceHigh,
> > neighborMatches]
>
> > I get an invalid SQL error: Mysql::Error: #42000You have an error in
> > your SQL syntax; check the manual that corresponds to your MySQL
> > server version for the right syntax to use near
> > '3,33,41,187,432,497,533,537,558,561,564,245,274,365,474,534,536,577,4,18,235,346'
> > at line 1: SELECT * FROM properties WHERE ((NO_BEDROOMS > '1' AND
> > NO_FULL_BATHS > '1' AND SQUARE_FEET > '500' AND LIST_PRICE > '0' AND
> > LIST_PRICE < '99999999' AND id IN
> > 3,33,41,187,432,497,533,537,558,561,564,245,274,365,474,534,536,577,4,18,235,346,396,439,509,66,468))
>
> > Can anyone spot the SQL syntax error?  Thanks in advance!!
>
> > LAB


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---