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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:43:21+00:00 2026-06-01T00:43:21+00:00

I have a SQL query that I’m trying to write, but I’m not quite

  • 0

I have a SQL query that I’m trying to write, but I’m not quite sure how to get it to work.

I have three tables: “s”, “t”, and “st” (which is a map between “s” and “t”.

table s
=======
primary key sID   |   val
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
       a              0
       b              1
       c              5
       d              6
       e              7



table t
=======
primary key tID   |   val
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
      nul               -1
      bbb                2
      ccc                3
      ddd                4



table st
========
foreign key sID   |  foreign key tID
(unique)          |  (multiple sID to one tID, meaning tID is not unique)
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
       a                    ddd
       b                    ccc
       c                    nul
       d                    ccc
       e                    bbb

So, all ‘s’ have to be mapped to a ‘t’, but ones which are not at a real ‘t’ are instead mapped to the default/null ‘t’ (nul).

The val’s are unique across both ‘s’ and ‘t’, meaning that if table ‘s’ has a 1, then table ‘t’ cannot have a 1.


So my problem is the following:

Given a set of vals (which can be either ‘s’ or ‘t’), I need to get the sID and tID in the ‘st’ table of the corresponding IDs. The problem is that if a ‘s’ is in the set but it’s ‘t’ is NOT in the set, I need to get the values (sID, ‘nul’) rather than (sID, tID).

For example, given the values (3, 1, 6), it would return the pairs: (b,ccc); (d,ccc);

Given the values (0,4), it would return the pair: (a,ddd)

However, given the values (6), it would need to return (d, nul) since the val 3 (which corresponds to ccc which is what d is mapped to) is not in the set. I don’t need null ‘s’ though, just null ‘t’.


I was thinking of using the following statement, but that doesn’t help me with returning “id, nul” if only the ‘s’ is in the set but not ‘t’.

SELECT st.sID, st.tID FROM t, st WHERE t.tID=st.tID AND (t.val=%_VAL1_% OR t.val=%_VAL_2 OR ......);

That gives me anything that has both a ‘t’ and a ‘s’ in the set (really it gives me all ‘s’ associated with any ‘t’ that are in the set), but it doesn’t give me the ‘s’ that are by themselves.

Perhaps I could post-process anything that’s in ‘s’ but not in ‘st’, but I’d like to avoid that if possible.

Does anyone have any suggestions? I’m rather stuck.

Thanks!

(note: s, t, and st are not my real table names, don’t worry. Also, the primary key’s are actually text GUIDs, unfortunately, but I tried to make it simpler to distinguish)

  • 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-06-01T00:43:23+00:00Added an answer on June 1, 2026 at 12:43 am

    Here is what you want:

    SELECT s.sID, t.tID
    FROM st
        LEFT JOIN s
            ON s.sID = st.sID AND s.val IN (6)
        LEFT JOIN t
            ON t.tID = st.tID AND t.val IN (6)
    WHERE NOT(s.sid IS NULL AND t.tid IS NULL)
    

    Here is the SQLFiddle that proves it for all 3 scenarios. You have to have the mapping table as the main, since the resultset is from tables that can be null.

    Although, this is very weird logic. Usually if a mapping is not hit in many-to-many relationship, you just return 0 rows, not a partial….

    If you want to cancel out the duplicates that might occur from multiple mapping misses (null in one column output), then add a DISTINCT. Like this:

    SELECT DISTINCT s.sID, t.tID
    FROM st
        LEFT JOIN s
            ON s.sID = st.sID AND s.val IN (6)
        LEFT JOIN t
            ON t.tID = st.tID AND t.val IN (6)
    WHERE NOT(s.sid IS NULL AND t.tid IS NULL)
    

    Here is the fiddle that shows the duplication

    And, here is the one that fixes it

    AND Finally, based on latest update. If you want any NULL s columns to be excluded, just make s an INNER JOIN and you can then remove the NULL check since you will not get double NULL’s now…Feel free to remove the distinct if you do want multiple sID|NULL results.

    SELECT DISTINCT s.sID, t.tID
    FROM st
        JOIN s
            ON s.sID = st.sID AND s.val IN (6)
        LEFT JOIN t
            ON t.tID = st.tID AND t.val IN (6)
    

    Here is the SQLFiddle

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

Sidebar

Related Questions

I have a sql query that runs super fast, around one second, when not
I have a SQL query that I'm trying to debug. It works fine for
I have an SQL query that uses GROUP_CONCAT to get all people attached to
I have a sql query that is retrieving results from a table but I
I have an SQL Query that i'm running but I only want to select
I have a SQL query that is quite simply select * from tblOrders where
I have a working sql query that I have been trying to convert to
I have an sql query that returns rows from two tables with same column
Ok, I have a SQL query that I'm trying to generate that will combine
I have a SQL query that seems to be producing correct results but according

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.