Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9120441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:37:43+00:00 2026-06-17T05:37:43+00:00

Using Sequel I’d like to join two subqueries together that share some column names,

  • 0

Using Sequel I’d like to join two subqueries together that share some column names, and then table-qualify those columns in the select.

I understand how to do this if the two datasets are just tables. E.g. if I have a users table and an items table, with items belonging to users, and I want to list the items’ names and their owners’ names:

@db[:items].join(:users, :id => :user_id).
  select{[items__name, users__name.as(user_name)]}

produces

SELECT "items"."name", "users"."name" AS "user_name" 
  FROM "items" 
INNER JOIN "users" ON ("users"."id" = "items"."user_id")

as desired.

However, I’m unsure how to do this if I’m joining two arbitrary datasets representing subqueries (call them my_items and my_users)

The syntax would presumably take the form

my_items.join(my_users, :id => :user_id).
  select{[ ... , ... ]}

where I would supply qualified column names to access my_users.name and my_items.name. What’s the appropriate syntax to do this?

A partial solution is to use t1__name for the first argument, as it seems that the dataset supplied to a join is aliased with t1, t2, etc. But that doesn’t help me qualify the item name, which I need to supply to the second argument.

I think the most desirable solution would enable me to provide aliases for the datasets in a join, e.g. like the following (though of course this doesn’t work for a number of reasons)

my_items.as(alias1).join(my_users.as(alias2), :id => :user_id).
  select{[alias1__name, alias2__name ]}

Is there any way to do this?

Thanks!

Update

I think from_self gets me part of the way there, e.g.

my_items.from_self(:alias => :alias1).join(my_users, :id => :user_id).
  select{[alias1__name, t1__name]}

seems to do the right thing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:37:44+00:00Added an answer on June 17, 2026 at 5:37 am

    OK, thanks to Ronald Holshausen’s hint, got it. The key is to use .from_self on the first dataset, and provide the :table_alias option in the join:

    my_items.from_self(:alias => :alias1).
      join(my_users, {:id => :user_id}, :table_alias => :alias2).
      select(:alias1__name, :alias2__name)
    

    yields the SQL

          SELECT "alias1"."name", "alias2"."name" 
            FROM ( <my_items dataset> ) AS "alias1" 
      INNER JOIN ( <my_users dataset> ) AS "alias2"
              ON ("alias2"."id" = "alias1"."user_id")
    

    Note that the join hash (the second argument of join) needs explicit curly braces to distinguish it from the option hash that includes :table_alias.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm considering using Sequel for some of my hairier SQL that I find too
i am using sequel pro on MACOSX and i've defined the table containing the
Using the Sequel gem: employees = DB[:prm_master__employee.identifier] .join(:prm_master__employee_custom_fields.identifier, :employee => :employee) .where(termination_date >= ?,06/01/2012)
I'm using the sequel ruby orm with sql server. I would like to know
I am not using Rails, but I'm using Sinatra, Sequel and some asynchronous messaging
when i am using sequel pro to connect to a remote database, the server
I just started using Sequel in a really small Sinatra app. Since I've got
when I am using 'localhost' as the host for MySQL database, sequel pro alert
Using a populated Table Type as the source for a TSQL-Merge. I want to
I'm using the Sequel (Taps) ruby gem for a remote backup of my production

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.