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

  • Home
  • SEARCH
  • 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 498057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:50:27+00:00 2026-05-13T05:50:27+00:00

I’m trying to join two table (call them table1 and table2) but only return

  • 0

I’m trying to join two table (call them table1 and table2) but only return 1 entry for each match. In table2, there is a column called ‘current’ that is either ‘y’, ‘n’, or ‘null’. I have left joined the two tables and put a where clause to get me the ‘y’ and ‘null’ instances, those are easy. I need help to get the rows that join to rows that only have a ‘n’ to return one instance of a ‘none’ or ‘null’. Here is an example

table1
ID
1
2
3

table2
ID | table1ID | current
1 | 1 | y
2 | 2 | null
3 | 3 | n
4 | 3 | n
5 | 3 | n

My current query joins on table1.ID=table2.table1ID and then has a where clause (where table2.current = ‘y’ or table2.current = ‘null’) but that doesn’t work when there is no ‘y’ and the value isn’t ‘null’.

Can someone come up with a query that would join the table like I have but get me all 3 records from table1 like this?

Query Return

ID | table2ID | current
1 | 1 | y
2 | null | null
3 | 3 | null or none

  • 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-13T05:50:28+00:00Added an answer on May 13, 2026 at 5:50 am

    First off, I’m assuming the “null” values are actually strings and not the DB value NULL.
    If so, this query below should work (notice the inclusing of the where criteria INSIDE the ON sub-clause)

    select 
    table1.ID as ID
    ,table2.ID as table2ID
    ,table2.current 
    from table1 left outer join table2 
    on (table2.table1ID = table1.ID and 
    (table2.current in ('y','null'))
    

    If this does work, I would STRONGLY recommend changing the “null” string value to something else as it is entirely misleading… you or some other developer will lose time debugging this in the future.

    If “null” acutally refers to the null value, then change the above query to:

    select 
    table1.ID as ID
    ,table2.ID as table2ID
    ,table2.current 
    from table1 left outer join table2 
    on (table2.table1ID = table1.ID and 
    (table2.current = 'y' or table2.current is null))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

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.