# Mysql query is not working

**URL:** <https://www.ruby-forum.com/t/mysql-query-is-not-working/167950>\
**Category:** Rails\
**Created:** [May 14, 2009, 11:34am UTC](https://www.ruby-forum.com/t/mysql-query-is-not-working/167950 "2009-05-14T11:34:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![venkat\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4af34b/32.png) [@venkat\_ruby](https://www.ruby-forum.com/u/venkat_ruby)\
**Post date:** [May 14, 2009, 11:34am UTC](https://www.ruby-forum.com/t/mysql-query-is-not-working/167950/1 "2009-05-14T11:34:36Z")

</div>

Please,

Can anyone help me to execute this query?

SELECT \* FROM table WHERE customer\_id IN (SELECT IF(1 \<\> 2,‘SELECT  
customer\_id FROM customers’,‘SELECT customer\_id FROM company’))

Regards…

---

<div class="post-metadata">

**Author:** ![venkat\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4af34b/32.png) [@venkat\_ruby](https://www.ruby-forum.com/u/venkat_ruby)\
**Post date:** [May 14, 2009, 3:30pm UTC](https://www.ruby-forum.com/t/mysql-query-is-not-working/167950/2 "2009-05-14T15:30:29Z")

</div>

On May 14, 2009, at 5:34 AM, Venkat E. wrote:

> Please,
> 
> Can anyone help me to execute this query?
> 
> SELECT \* FROM table WHERE customer\_id IN (SELECT IF(1 \<\> 2,‘SELECT  
> customer\_id FROM customers’,‘SELECT customer\_id FROM company’))
> 
> ## Regards…

No!

It looks like you’re trying to do either:

SELECT table.\* FROM table INNER JOIN customers ON  
customers.customer\_id = table.customer\_id

or:

SELECT table.\* FROM table INNER JOIN company ON company.customer\_id =  
table.customer\_id

So you can do either of those (after you substitute for ‘table’ of  
course)

If this is Rails (ActiveRecord), then ask your question again with  
some more background and perhaps a bit of code (not SQL) and you’ll  
get a better answer. (And that answer will probably show you how to  
use has\_many and belongs\_to associations in your models.)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![venkat\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4af34b/32.png) [@venkat\_ruby](https://www.ruby-forum.com/u/venkat_ruby)\
**Post date:** [May 15, 2009, 6:50am UTC](https://www.ruby-forum.com/t/mysql-query-is-not-working/167950/3 "2009-05-15T06:50:57Z")

</div>

Rob B. wrote:

> On May 14, 2009, at 5:34 AM, Venkat E. wrote:
> 
> > Please,
> > 
> > Can anyone help me to execute this query?
> > 
> > SELECT \* FROM table WHERE customer\_id IN (SELECT IF(1 \<\> 2,‘SELECT  
> > customer\_id FROM customers’,‘SELECT customer\_id FROM company’))
> > 
> > ## Regards…
> 
> No!
> 
> It looks like you’re trying to do either:
> 
> SELECT table.\* FROM table INNER JOIN customers ON  
> customers.customer\_id = table.customer\_id
> 
> or:
> 
> SELECT table.\* FROM table INNER JOIN company ON company.customer\_id =  
> table.customer\_id
> 
> So you can do either of those (after you substitute for ‘table’ of  
> course)
> 
> If this is Rails (ActiveRecord), then ask your question again with  
> some more background and perhaps a bit of code (not SQL) and you’ll  
> get a better answer. (And that answer will probably show you how to  
> use has\_many and belongs\_to associations in your models.)
> 
> -Rob
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

Thanks… Rob B.

I got the solution for that…below code is working fine for me… 🙂

SELECT \* FROM books WHERE customer\_id IN (SELECT customer\_id FROM  
customers WHERE (IF(‘#{user\_type}’ \<\> 2,customer\_id IS NOT  
NULL,company\_id=‘“cus01”’)))

---

<div class="post-metadata">

**Author:** ![venkat\_ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4af34b/32.png) [@venkat\_ruby](https://www.ruby-forum.com/u/venkat_ruby)\
**Post date:** [May 16, 2009, 8:16am UTC](https://www.ruby-forum.com/t/mysql-query-is-not-working/167950/4 "2009-05-16T08:16:58Z")

</div>

Venkat E. wrote:  
[…]

> Thanks… Rob B.
> 
> I got the solution for that…below code is working fine for me… 🙂

You completely missed Rob’s point. If you’re using Rails, then in most  
cases you should be letting ActiveRecord write the SQL, _not writing SQL  
yourself_!

> SELECT \* FROM books WHERE customer\_id IN (SELECT customer\_id FROM  
> customers WHERE (IF(‘#{user\_type}’ \<\> 2,customer\_id IS NOT  
> NULL,company\_id=‘“cus01”’)))

This is a waste of time (and the syntax is wrong). What you want is  
probably something like

if user\_type == 2  
books = Customer.find\_all\_by\_company\_id(‘cus01’).books  
else  
books = Customer.find(:all, :conditions =\> ‘company\_id is not  
null’).books  
end

See? Much cleaner. If you’re going to use Rails, take the time to  
learn how to work _with_ it, not against it.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
