简体   繁体   中英

IF ELSE statement to join table sql

I have one supertype table where I have to pick 1 subtype table from 2 subtypes a,b. A subtype cannot go with the other one so for me to query I have to check whether if the supertype id is contained on one of the subtypes. I have been doing experiment queries but cannot get it right.

This is what somehow I thought of:

SELECT * from supertypetable INNER JOIN 
IF  (a.id = given.id) then a ON a.id = supertypetable.id
ELSE  b ON b.id = supertetable.id

job Table
________________________________
|job_id| blach2x....
________________________________
|    1  |
|    2  |
|    3  |
________________________________

partime Table
________________________________
|job_id| blach2x....
________________________________
|    2  |
|    3  |
________________________________

fulltime Table
________________________________
|job_id| blach2x....
________________________________
|    1  |
|       |
________________________________

I want to join tables that satisfy my given id

This looks a lot like a polymorphic join in rails/activerecord. The way it's implemented there, the 'supertype' table has two fields: subtype_id and subtype_type. The subtype_type table has a string that can be easily turned into the name of the right subtype table; subtype_id has the id of the row in that table. Structuring your tables like this might help.

The next question you have to ask is what exactly are you expecting to see in the results? If you want to see the supertype table plus ALL of the subtype tables, you're probably going to have to join them one at a time, then union them all together. In other words, first join against just one of the subtype tables, then against the next one, etc. If this isn't what you're going for, maybe you could clarify your question further.

If a.id can never equal b.id you could do joing on both tables and then do a UNION and only the table where the id matched would return results:

SELECT * from supertypetable 
INNER JOIN 
a ON a.id = supertypetable.id
UNION
SELECT * from supertypetable 
INNER JOIN 
b ON b.id = supertypetable.id

If a.id can equal b.id, then this would not work. But it's an idea

EDITTING PER COMMENTS: This approach only works if the structures of a and b are identical.

So one simple suggestion might be just:

   SELECT * FROM job
    left join parttime on parttime.job_id = job.job_id
    left join fulltime on fulltime.job_id = job.job_id
where job.job_id = @job_id

And then let your application figure out which of the two joined tables doesn't have NULL data and display that.

If you don't mind inconsistent datasets and just always want the correct returned set regardless (although you're still going to need some kind of application logic since as you said, the structures of parttime and fulltime are different, so how are you going to display/utilize their data conditionally without some kind of inspection? And if you're going to do that inspection, you might as well do it up front, figure out for your given job_id what the subtype is, and then just pick the appropriate query to run there.)

Sorry! Digression!

A stored procedure can do this logic for you (removed all the joins, just an example):

CREATE PROCEDURE getSubTypeDATA (@job_id int)
BEGIN

IF (SELECT EXISTS(SELECT 1 FROM parttime WHERE job_id = @job_id)) = 1
BEGIN
SELECT * from parttime
END
ELSE
BEGIN
SELECT * from fulltime
END

END

Alternatively, if you want a consistent dataset (ideal for application logic), why not put the common columns between fulltime and parttime into a UNION statement, and create hard-coded NULLs for the columns they don't share. For example, if fullTime looked like

EmployeeID, DepartmentID, Salary

and partTime looked like

EmployeeID, DepartmentID, HourlyRate

you could do

SELECT job.*, employeeid, departmentid, salary, null as hourlyrate FROM job inner join fulltime on fulltime.job_id = job.job_id
where job.job_id = ?
union
  SELECT job.*, employeeid, departmentid, null as salary, hourlyrate FROM job inner join parttime on parttime.job_id = job.job_id
where job.job_id = ?

If there are hundred different columns, this might be unwieldy. Also, in case this didn't make it obvious, having subtypes with completely different structures but using the same foreign key is a very good clue that you're breaking third normal form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM