# SQL query problem

**URL:** <https://www.ruby-forum.com/t/sql-query-problem/180351>\
**Category:** Rails\
**Created:** [December 18, 2009, 6:17am UTC](https://www.ruby-forum.com/t/sql-query-problem/180351 "2009-12-18T06:17:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [December 18, 2009, 6:17am UTC](https://www.ruby-forum.com/t/sql-query-problem/180351/1 "2009-12-18T06:17:49Z")

</div>

Please help me with finding a SQL select statement for the following  
problems.

I have two classes: Site and Link

Table Link has two columns: linking\_site\_id, and linked\_site\_id

I would like to find all the links in table Link where linking\_site\_id  
= 3 or linked\_site\_id = 3 but only include a link where linked\_site\_id  
= 3 if the linking\_site\_id in that link is not one of the  
linked\_site\_id that site 3 links to. For example, if the Link table  
contains the following rows

Linking\_site Linked\_site  
2 1  
3 2  
3 4  
5 3  
2 3

What SQL select statement would return only the following:

3 2  
3 4  
5 3

If SQL query alone cannot do this, what is the most efficient way to  
do it via RoR? Thanks.

Thanks.

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Core” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-core?hl=en](http://groups.google.com/group/rubyonrails-core?hl=en).

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [December 18, 2009, 9:10am UTC](https://www.ruby-forum.com/t/sql-query-problem/180351/2 "2009-12-18T09:10:44Z")

</div>

Hi,

Learn by Doing wrote:

> Please help me with finding a SQL select statement for the following  
> problems.

> Linking\_site Linked\_site  
> 2 1  
> 3 2  
> 3 4  
> 5 3  
> 2 3
> 
> What SQL select statement would return only the following:
> 
> 3 2  
> 3 4  
> 5 3

TableName.find\_by\_sql(“select Linking\_site\_id, Linked\_site\_id from table  
t where Linking\_site\_id=3 or (Linked\_site\_id=3 and Linking\_site\_id in  
(select distinct(Linking\_site\_id) from table t2 where Linking\_site\_id=3  
)”)

Hope that helps 🙂

Regards,

Salil G.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [December 21, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/sql-query-problem/180351/3 "2009-12-21T15:25:30Z")

</div>

select \*  
from link\_table le  
where le.link\_site = ‘3’ or le.linked\_site =‘3’  
and not exists (select 0  
from link\_table li  
where li.link\_site = le.linked\_site  
and le.link\_site = li.linked\_site)

2009/12/18 Ease B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> linked\_site\_id that site 3 links to. For example, if the Link table

–

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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [December 21, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/sql-query-problem/180351/4 "2009-12-21T15:26:05Z")

</div>

select \*  
from link\_table le  
where le.link\_site = ‘3’ or le.linked\_site =‘3’  
and not exists (select 0  
from link\_table li  
where li.link\_site = le.linked\_site  
and le.link\_site = li.linked\_site)

2009/12/18 Ease B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> linked\_site\_id that site 3 links to. For example, if the Link table

–

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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [December 30, 2009, 2:46am UTC](https://www.ruby-forum.com/t/sql-query-problem/180351/5 "2009-12-30T02:46:12Z")

</div>

Thanks everyone for your help! Happy New Year.

On Dec 21, 6:24 am, Roberta [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Please help me with finding a SQL select statement for the following  
> > contains the following rows  
> > 3 2  
> > You received this message because you are subscribed to the Google G.  
> > “Ruby on Rails: Talk” group.  
> > To post to this group, send email to removed\_email\_address@domain.invalid.  
> > To unsubscribe from this group, send email to  
> > removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > .  
> > For more options, visit this group at  
> > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–

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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
