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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:57:43+00:00 2026-05-25T15:57:43+00:00

The database I am working with currently does have two tables, one holding the

  • 0

The database I am working with currently does have two tables, one holding the attribute “contact_person”. This attribute is compared to several attributes (not tuples!) in another table which are named “contact1” to “contact4” to determine what other attributes to display from the second table (i.e. “email1” to “email4”). So the first table only holds the contact person, the second the actual address data for several contact persons (again, all of them in a row or a single tuple). Do not tell me that this aint proper database design – I have to work with what I was given -.-

To get the proper contact data I chose to perform a select for each comparison and UNION them. This works fine and executes well enough for the application that performs the query. It looks, very simplified, something like this:

SELECT contact1, email1, phone1 FROM T1,T2 WHERE contact_person = contact1
UNION ALL
SELECT contact2, email2, phone2 FROM T1,T2 WHERE contact_person = contact2
UNION ALL
SELECT contact3, email3, phone3 FROM T1,T2 WHERE contact_person = contact3
UNION ALL
SELECT contact4, email4, phone4 FROM T1,T2 WHERE contact_person = contact4

Now, due to malformed and non-error-checked INSERT queries, it is possible that “contact_person” is NULL or an empty string or that any of the “contactN” attributes is NULL. So I need another SELECT query that displays all records that are in the database but are not in the record-set yet.

With the given example that could be achieved using something similar to this:

UNION
SELECT contact_person, 'N/A', 'N/A' FROM T1,T2

In the real life query I (need to) do a lot of formatting of “contact_person” and the “contactN”s, so simply doing a UNION (without the ALL, so equal records get excluded) wont work (contactN is actually a multi field value and the formatting is not consistent, so the records returned by the last query might differ from the records above, even for the same entry in the database). Also, the query is so enormous already that it is no option to use the opposite of the first query to exclude its records, like so:

UNION ALL
SELECT contact_person, 'N/A', 'N/A' FROM T1,T2
    WHERE contact_person <> contact1
    AND contact_person <> contact2
    AND contact_person <> contact3
    AND contact_person <> contact4

So is there another way to display all records that have not been selected with the very first query posted above? Maybe by somehow running a subquery (ofc there does exists a UID for the returned records – I just dont know how to work with it in this context)? Could the very first query have been written in a simpler way?

  • 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-25T15:57:44+00:00Added an answer on May 25, 2026 at 3:57 pm
    ;WITH cp AS
    (
        -- strongly recommend appropriate table prefixes in select list:
        SELECT UID, contact1, email1, phone1 
        -- also strongly recommend proper inner joins:
            FROM T1 INNER JOIN T2 ON t1.contact_person = t2.contact1
        UNION ALL
        SELECT UID, contact2, email2, phone2 
            FROM T1 INNER JOIN T2 ON t1.contact_person = t2.contact2
        UNION ALL
        SELECT UID, contact3, email3, phone3
            FROM T1 INNER JOIN T2 ON t1.contact_person = t2.contact3
        UNION ALL
        SELECT UID, contact4, email4, phone4 
            FROM T1 INNER JOIN T2 ON t1.contact_person = t2.contact4
    )
    SELECT contact1, email1, phone1 FROM cp
    UNION ALL
    SELECT t1.contact_person, 'N/A', 'N/A'
    FROM T1 INNER JOIN T2 ON t1.contact_person = t2.contact1
    WHERE UID NOT IN (SELECT UID FROM cp);
    

    You might also consider a more normalized/relational design?

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

Sidebar

Related Questions

I'm currently working on a wcf service that does some lookups in a database
I'm currently working with the versant object database (using jvi), and have a case
I'm currently working on a DNA database class and I currently associate each row
A database application that I'm currently working on, stores all sorts of settings in
I'm currently working on someone else's database where the primary keys are generated via
I'm currently working on a large implementation of Class::DBI for an existing database structure,
I’m currently working on a classic ASP project talking to an Oracle database. I’m
I'm currently working on a PHP application that uses a MySQL database for its
I have a database working in my local sql server 2005 express edition. I
this is a different question concerning: add a connection to database not working, asp.net

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.