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
In Process

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?

  • 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. 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

Related Questions

I have a simple query over a table, which returns results like the following:
The following query returns the total amount of orders, per week, for the past
The following query returns values for months with sales but not for months where
I have the following query which returns a list of questions and the possible
The prepared query returns the following resultset, when $this->show is set to saved-by-the-bell :
I have the following MQL query which successfully returns the record for William Shakespeare.
I am running the following query and checking whether it returns any results and
I would like to create a query that returns rows in the following format:
The following query looks the table ids and return the id if for example
I have the following query select main_cat_name,cat_url from mf_main order by main_cat_name This returns

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.