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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:36:46+00:00 2026-05-24T19:36:46+00:00

I was trying to retrieve value from two tables by joining, but few values

  • 0

I was trying to retrieve value from two tables by joining, but few values in the second columns missing.

eg

valuetable

----------------------
| id |orderid| prodid|
----------------------
|  1 |  NULL |      4|
----------------------
|  1 |     A |      4|
----------------------
|  2 |  NULL |      4|
----------------------
|  2 |  NULL |      3|
----------------------
|  3 |     B |      4|
----------------------
|  3 |     B |      3|
----------------------
|  3 |     B |      5|
----------------------

Nametable

--------------
| id |   Name|
--------------
|  1 |  Apple|
--------------
|  2 |   Ball|
--------------
|  3 |    Cat|
--------------
|  4 |    Dog|
--------------

I have the above two table. I have to retrieve values in the below format.

Query Result

----------------------
| id |   Name|orderid|
----------------------
|  1 |  Apple|  NULL |
----------------------
|  2 |   Ball|  NULL |
----------------------
|  3 |    Cat|     B |
----------------------
|  4 |    Dog|  NULL |
----------------------

I have used the following query

SELECT  nt.id, nt.[Name]
    , MIN(vt.orderid) AS orderid
FROM   Nametable nt  LEFT OUTER JOIN valuetable vt 
ON nt.id = vt.id
WHERE 
    vt.prodid = 3
GROUP BY
    nt.id, nt.[Name]

but missing the | 1 | Apple| NULL | row. How can I retrieve it?
using MSSQL Server 2005

UPDATE

value table doesn’t have id value 1 for prodid=3 but I need a result in the above mentioned way. means if it cannot get id from the valuetable for the corresponding prodid it should still retrieve the id and its name with NULL value as orderid.

  • 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-24T19:36:47+00:00Added an answer on May 24, 2026 at 7:36 pm

    Seems you have

    WHERE vt.prodid = 3
    

    valuetable does not contain an Apple with prodid = 3

    So you should not have in the where but in the join clause:

    SELECT  nt.id, nt.[Name]
    , MIN(vt.orderid) AS orderid
    FROM   Nametable nt  LEFT OUTER JOIN valuetable vt 
    ON nt.id = vt.id AND vt.prodid = 3
    GROUP BY
      nt.id, nt.[Name]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using PHP. I'm trying to retrieve $_POST values from a the second dimension of
I am trying to retrieve a value from an Array. Here is the code:
I am trying to retrieve the correct value from an ArrayList of objects (.NET
Iam trying to retrieve data from mysql database into stylesheet.php but it is not
I am trying to retrieve a set of values from database and assigned it
I am trying to write a SQL query where I am joining two tables
I am trying to store/retrieve a value that is stored in the Application Settings.
I'm trying to retrieve mulitple checked value, which are being showed in JSP dynamically,
I'm trying to retrieve a file from a server using SFTP (as opposed to
I'm trying to retrieve an XML stream from a URL. For most URLs my

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.