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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:14:52+00:00 2026-05-12T05:14:52+00:00

I need to select data from two table using a join. This is fairly

  • 0

I need to select data from two table using a join. This is fairly simple and have no problems here. The problem occurs when the field I am joining is used as two separate foreign keys (I didn’t design this). So the ID field that I join on is either a positive or negative number.

If it’s a positive number it relates to ID_1 on the table_2 table, if it’s a negative, the number relates to ID_2 on the table_2 table. However the ID_2 will be a positive number (even though it’s stored as a negative in the foreign key). Obviously there are no constraints to enforce these – so in essence not real foreign keys :/

The SQL I’m using goes something like this and is fine for the positive numbers:

select t1.Stuff, t2.MoreStuff from table_1 t1
join table_2 t2 on t1.ID_1 = t2.ID_1
where ...

How to incorporate the negative aspect of this into the join. Is this even possible? Ideally I’d like to alter the table to my needs but apparently this is not a valid option. I’m well and truly stuck.

The only other idea I’ve had is a separate sql statement to handle these odd ones. This is all being run by clr sql from C#. Adding a separate SqlCommand to the code will most likely slow things down hence why I’d prefer to keep it all in one command.

Your input is welcome, thanks 🙂

  • 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-12T05:14:52+00:00Added an answer on May 12, 2026 at 5:14 am

    Let’s say the tables look like this:

    Table1 (id INT, foo INT, fk INT)
    
    Table2 (id1 INT, id2 INT, bar VARCHAR(100))
    

    …where fk can be used to look up a row in Table2 using id1 if positive and id2 if negative.

    Then you can do the join as follows:

    SELECT T1.id, T1.foo, T2.bar
    FROM Table1 T1 INNER JOIN Table2 T2
    ON    (T1.fk > 0 AND T2.id1 = T1.fk)
       OR (T1.fk < 0 AND T2.id2 = - T1.fk)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 512k
  • Answers 512k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try adding additional fields to the original table and using… May 16, 2026 at 5:45 pm
  • Editorial Team
    Editorial Team added an answer ArrayList q = new ArrayList(); foreach (object o in listBox4.Items)… May 16, 2026 at 5:45 pm
  • Editorial Team
    Editorial Team added an answer From man mq_send on linux (emphasis added): The msg_prio argument… May 16, 2026 at 5:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am using ActiveRecord in Rails 3 to pull data from two different tables
please I have the same problem as I found here MySQL - Selecting data
I've used this MySQL query to get data: SELECT DISTINCT A.*, B.username FROM posts
At the moment, I select rows from 'table01 and table02' using: SELECT t1.*,t2.* FROM
I am new to ADO.net and have this problem: Let's assume I have these
Here's what I have. I have an MVC application where all the data is
I'm fairly new to NHibernate, and I need to ask a couple of questions
Let's say I need to query the associates of a corporation. I have a
(Advantage Database Server) I have a table of service providers that, for auditing purposes,
When I right click on the database I want to export data from, I

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.