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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:00:19+00:00 2026-05-26T02:00:19+00:00

I have such query. It returns ColA and ColB from TableA and UserName from

  • 0

I have such query. It returns ColA and ColB from TableA and UserName from table Users. Then it displays several fields from TableB as additional columns to results. It works but is there any better way than using these multiple LEFT JOINS ?

SELECT a.COlA, a.ColB, u.UserName,
b1.Value,
b2.Value,
b3.Value,
b4.Value,

FROM TableA a JOIN Users u ON a.UserId = u.UserId
    LEFT JOIN TableB b1 ON a.EventId = b1.EventId AND b1.Code = 5
    LEFT JOIN TableB b2 ON a.EventId = b2.EventId AND b2.Code = 15
    LEFT JOIN TableB b3 ON a.EventId = b3.EventId AND b3.Code = 18
    LEFT JOIN TableB b4 ON a.EventId = b4.EventId AND b4.Code = 40

WHERE (a.UserId = 3) ORDER BY u.UserName ASC

TableB looks like:

Id | EventId | Code | Value
----------------------------
1  |    1    |  5   | textA
2  |    1    |  15  | textB
3  |    1    |  18  | textC

Sometimes Code is missing but for each event there are no duplicated Codes (so each LEFT JOIN is just another cell in the same result record).

  • 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-05-26T02:00:20+00:00Added an answer on May 26, 2026 at 2:00 am

    I cannot understand why you want to change something that is working, but here’s another way (which does those LEFT joins, but in a different way):

    SELECT a.COlA, a.ColB, u.UserName,
      ( SELECT b.Value FROM TableB b WHERE a.EventId = b.EventId AND b.Code = 5 ),
      ( SELECT b.Value FROM TableB b WHERE a.EventId = b.EventId AND b.Code = 15 ),
      ( SELECT b.Value FROM TableB b WHERE a.EventId = b.EventId AND b.Code = 18 ),
      ( SELECT b.Value FROM TableB b WHERE a.EventId = b.EventId AND b.Code = 40 )
    
    FROM TableA a JOIN Users u ON a.UserId = u.UserId
    
    WHERE (a.UserId = 3) 
    
    ORDER BY u.UserName ASC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example I have such query: Query q = sess.createQuery(from Cat cat); List cats
If I have a query such as : select * from tbl where id
Say that I have LINQ query such as: var authors = from x in
I have a query on my database as such: SELECT * FROM expenses WHERE
I have following SQL query that selects some results from my table: select avg(c3),
I have a query to find certain customers from a table. SELECT COUNT(*) FROM
I have a very straight forward select query such as this: SELECT * FROM
I have a query such that I need to get A specific dog All
Is it possible to have a query such as the following: SELECT @threadIDvar =
I have some problem whith such mysql_query INSERT INTO table VALUES ('', CURDATE()-1) why

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.