How can I get join to work with conditions

asdfasdf

I have a table TimeIntervals with a relationship to Breaks with in turn has a relationship to DeletedBreaks.

What i'm trying to do is to receive al rows (Breaks) that has timeIntervals id and no deleted break for a given date.

That is if a break has no deleted breaks, no row in DeletedBreaks table for a break id Or if there is a row with that breaks id but not the given date, than that break should be returned.

Following is not working but you might understand what i'm trying to do:

SELECT B.*
FROM Breaks B
JOIN TimeIntervals T
    ON B.TimeIntervalId = T.Id
JOIN DeletedBreaks DB
    ON (
            (
                DB.BreakId = B.Id
                AND DB.DeletedDate <> '2014-10-13'
                )
            OR DB.BreakId IS NULL
            )
        AND (T.Id = 2)
JNevill

Use a LEFT JOIN to your DeletedBreaks table instead of an inner join since you don't want to drop Break records just becasue the DeletedBreaks ID is null.

To test for NULL DeletedBreaks or DeletedBreaks for a particular day, do so in the WHERE clause:

SELECT B.*
FROM Breaks B
JOIN TimeIntervals T
    ON B.TimeIntervalId = T.Id
LEFT JOIN DeletedBreaks DB ON               
    DB.BreakId = B.Id 
WHERE
    (DB.DeletedDate <> '2014-10-13'
    OR DB.BreakId IS NULL)
    AND T.Id = 2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MYSQL - How can I get this left join to work?

From Dev

How can I apply LIMIT/ORDER conditions to an ActiveRecord Join?

From Dev

I can't get INNER JOIN to work "ORA 00918"

From Dev

How can I get a LEFT JOIN or FULL OUTER JOIN?

From Dev

How can I get SweetFX to work with RadeonPro?

From Dev

How can I get jshint to work?

From Dev

How can I get 'autodoc' work?

From Dev

How can I get FacetFilter work properly?

From Dev

How can I get at to work on OSX?

From Dev

How can I get .htaccess to work?

From Dev

How can I get ddclient to work with freedns?

From Dev

How can I get EFrepository to work?

From Dev

How can I get the following generator to work?

From Dev

How can I get this .slideToggle method to work?

From Dev

How can I get these queries to work together?

From Dev

How can i get results from another Entity with conditions?

From Dev

How do I join tables on multiple conditions

From Dev

How can I get a join to fill in missing values?

From Dev

how can I join these tables and get the desired results

From Dev

How can I get the source of the button and get this to work?

From Dev

How can I get the hostname of the machine I work on?

From Dev

How can I get my Python script to work using bash?

From Dev

How can i get work and age from Facebook iOS API

From Dev

How can I get SSRS and Chrome to work together?

From Dev

How can i get ngRoute to work in Cordova App

From Dev

How can I get Gradle ant logging to work?

From Dev

How can I get performance statistics to work in MySQL?

From Dev

How can I get the fgcolor attribute to work on recent Android versions?

From Dev

Lost work doing a git checkout, how can I get it back

Related Related

HotTag

Archive