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 146775

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:39:48+00:00 2026-05-11T08:39:48+00:00

The following query returns a single row, as desired. The ‘contracts’ table has 6

  • 0

The following query returns a single row, as desired. The ‘contracts’ table has 6 fields, each with a different username in it, for which I want to retrieve first/last names from a separate ‘users’ table. This works fine, but is there something more concise? I’m think the solution must be something using GROUP BY contracts.id to keep it one row, but I can’t seem to find anything better than this slew of sub-selects.

Help!

SELECT contracts.field1, contracts.field2,  (SELECT first_name FROM users WHERE username = service_provider_1), (SELECT last_name FROM users WHERE username = service_provider_1), (SELECT first_name FROM users WHERE username = service_provider_2), (SELECT last_name FROM users WHERE username = service_provider_2), (SELECT first_name FROM users WHERE username = service_org_business_contact), (SELECT last_name FROM users WHERE username = service_org_business_contact), (SELECT first_name FROM users WHERE username = client_service_contact_1), (SELECT last_name FROM users WHERE username = client_service_contact_1), (SELECT first_name FROM users WHERE username = client_service_contact_2), (SELECT last_name FROM users WHERE username = client_service_contact_2), (SELECT first_name FROM users WHERE username = client_business_contact), (SELECT last_name FROM users WHERE username = client_business_contact) FROM contracts WHERE id = ? 

It wouldn’t be so bad if I could get both first/last name from a single sub-select… so even with a cruddy sub-select solution I have twice the verbosity I should think I need…

EDIT: I get it now. The answer to being able to join to the same table multiple times is to use an alias for the table. Thank folks! New code is:

SELECT contracts.field1, contracts.field2,  sp1.first_name, sp1.last_name,  sp2.first_name, sp2.last_name,  sobc.first_name, sobc.last_name,  csc1.first_name, csc1.last_name,  csc2.first_name, csc2.last_name,  cbc.first_name, cbc.last_name FROM contracts JOIN users AS sp1 ON service_provider_1 = sp1.username JOIN users AS sp2 ON service_provider_2 = sp2.username JOIN users AS sobc ON service_org_business_contact = sobc.username JOIN users AS csc1 ON client_service_contact_1 = csc1.username JOIN users AS csc2 ON client_service_contact_2 = csc2.username JOIN users AS cbc ON client_business_contact = cbc.username WHERE contracts.id = ? 

Sadly, using joins is almost as verbose as using the subselects, but I assume it might be faster?

  • 0 0 Answers
  • 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. 2026-05-11T08:39:49+00:00Added an answer on May 11, 2026 at 8:39 am
    SELECT contracts.field1, contracts.field2,  sp1.first_name, sp1.last_name  sp2.first_name, sp2.last_name, /* etc, etc */ FROM contracts INNER JOIN users sp1 ON contracts.id = sp1.id AND sp1.username = service_provider_1 INNER JOIN  users sp2 ON contracts.id = sp2.id AND sp2.username = service_provider_2 INNER JOIN  users sobc ON contracts.id = sobc.id AND sobc.username = service_org_business_contact INNER JOIN /* etc, etc */ WHERE contracts.id = @myid 

    did you also want to combine first_name and last_name for each username? You can do this like

    RTRIM(sp1.first_name) + ' ' + RTRIM(sp1.last_name) as sp1_name 

    in your SELECT clause. The RTRIM are necessary if the data type is (N)CHAR, not necessary if the type is (N)VARCHAR

    EDIT: As stated in the comments on this answer, the JOIN on id is probably not necessary, in which case it becomes

    SELECT  contracts.field1,  contracts.field2,  sp1.first_name, sp1.last_name  sp2.first_name, sp2.last_name, /* etc, etc */ FROM contracts INNER JOIN users sp1 ON sp1.username = service_provider_1 INNER JOIN  users sp2 ON sp2.username = service_provider_2 INNER JOIN  users sobc ON  sobc.username = service_org_business_contact INNER JOIN /* etc, etc */ WHERE contracts.id = @myid 

    My layout probably makes it appear longer! You may need to use LEFT OUTER JOINS if it is possible to have a contract record that doesn’t have a first_name and last_name for one of it’s fields within the users table.

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

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use this method in Global.asax protected void Application_End() { } May 12, 2026 at 12:58 am
  • Editorial Team
    Editorial Team added an answer I finally found a solution (or at least a workaround)… May 12, 2026 at 12:58 am
  • Editorial Team
    Editorial Team added an answer Ordinarily it indicates a 404 error or some other error,… May 12, 2026 at 12:58 am

Related Questions

The following query returns a single row, as desired. The 'contracts' table has 6
I am looking for a way to retrieve the surrounding rows in a NHibernate
I'd like to use a single SQL query (in MySQL) to find the record
I have the following entity structure: Business --> Campaign --> Promotion, where ONE Business
I'd like to return an object with the following signature class AnonClass{ string Name

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.