On Oct 10, 2009, at 4:22 PM, Christer Nilsson wrote:
> Database A:
>
>   Humans
>     ID
>     Name
>
> Database B:
>
>   Pets
>     ID
>     Owner_ID
>     Name
> I would like to know the names of all humans with pets named Ajax.
>
> SELECT A.Humans.Name
> FROM A.Humans
> JOIN B.Pets ON B.Pets.Owner_ID = A.Humans.ID
> WHERE B.Pets.Name = 'Ajax'

These aren't two separate databases, they're two separate tables.

This doesn't answer your question directly, but perhaps it will get  
you started.

Slim2:~ phrogz$ irb
irb(main):001:0> require 'sequel'
=> true

irb(main):002:0> DB = Sequel.sqlite
=> #<Sequel::SQLite::Database: "sqlite:/">

irb(main):003:0> DB << "CREATE TABLE humans( id INTEGER PRIMARY KEY,  
name TEXT )"
=> nil

irb(main):004:0> DB << "CREATE TABLE pets( id INTEGER PRIMARY KEY,  
owner_id INTEGER, name TEXT )"
=> nil

irb(main):005:0> class Human < Sequel::Model( :humans )
irb(main):006:1>    one_to_many :pets, :key=>:owner_id
irb(main):007:1> end
=>  
{:key 
= 
 > 
:owner_id 
, :type 
= 
 > 
:one_to_many 
, :name 
= 
 > 
:pets 
, :cache 
= 
 > 
true 
, :model 
= 
 > 
Human 
, :eager_block 
= 
 > 
nil 
, :graph_join_type 
=>:left_outer, :order_eager_graph=>true, :graph_conditions=> 
[], :before_add=>[], :before_remove=>[], :after_add=> 
[], :after_remove=>[], :after_load=>[], :extend=> 
[], :class_name=>"Pet", :primary_key=>:id, :dataset=>#<Proc: 
0x000001011b5c38@/usr/local/lib/ruby/gems/1.9.1/gems/sequel-3.4.0/lib/ 
sequel/model/associations.rb:757>, :eager_loader=>#<Proc: 
0x000001011b5bc8@/usr/local/lib/ruby/gems/1.9.1/gems/sequel-3.4.0/lib/ 
sequel/model/associations.rb: 
761>, :cartesian_product_number=>1, :eager_grapher=>#<Proc: 
0x000001011b5b90@/usr/local/lib/ruby/gems/1.9.1/gems/sequel-3.4.0/lib/ 
sequel/model/associations.rb:782>, :orig_opts=> 
{:key=>:owner_id, :class_name=>"Pet", :class=>nil, :block=>nil}}

irb(main):008:0> class Pet < Sequel::Model
irb(main):009:1>    many_to_one :owner, :class=>:Human, :key=>:owner_id
irb(main):010:1> end
=>  
{:key 
= 
 > 
:owner_id 
, :type 
= 
 > 
:many_to_one 
, :name 
= 
 > 
:owner 
, :cache 
= 
 > 
true 
, :model 
= 
 > 
Pet 
, :eager_block 
= 
 > 
nil 
, :graph_join_type 
=>:left_outer, :order_eager_graph=>true, :graph_conditions=> 
[], :before_add=>[], :before_remove=>[], :after_add=> 
[], :after_remove=>[], :after_load=>[], :extend=> 
[], :class_name 
=>"Human", :cartesian_product_number=>0, :dataset=>#<Proc: 
0x0000010113d818@/usr/local/lib/ruby/gems/1.9.1/gems/sequel-3.4.0/lib/ 
sequel/model/associations.rb:713>, :eager_loader=>#<Proc: 
0x0000010113d7e0@/usr/local/lib/ruby/gems/1.9.1/gems/sequel-3.4.0/lib/ 
sequel/model/associations.rb:717>, :eager_grapher=>#<Proc: 
0x0000010113d7a8@/usr/local/lib/ruby/gems/1.9.1/gems/sequel-3.4.0/lib/ 
sequel/model/associations.rb:739>, :orig_opts=> 
{:class=>nil, :key=>:owner_id, :class_name=>"Human", :block=>nil}}

irb(main):011:0> Human << { :name=>"Gavin" }
=> 1

irb(main):012:0> Pet << { :owner_id=>1, :name=>"Fleep" }
=> 1

irb(main):013:0> Pet << { :owner_id=>1, :name=>"Tessa" }
=> 2

irb(main):014:0> gavin = Human[1]
=> #<Human @values={:id=>1, :name=>"Gavin"}>

irb(main):015:0> gavin.pets
=> [#<Pet @values={:id=>1, :owner_id=>1, :name=>"Fleep"}>, #<Pet  
@values={:id=>2, :owner_id=>1, :name=>"Tessa"}>]

irb(main):017:0> gavin.pets_dataset.sql
=> "SELECT * FROM `pets` WHERE (`pets`.`owner_id` = 1)"


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

Reply via email to