How to get all records from one table and only records from joined table with criteria

akabobo

it seems like I've accomplished this before, but struggling again. Here is my data:

tblHotlist
----------
ID
hotlistStatus
buildNumber
loadType
etc

tblessr
-------
ID
esHeadline
notesStatus
actionItems
bugStatusID
etc

tblBugStatus (not needed in query)
------------
ID
bugStatus
etc

tbl_j_hlbug
-----------
esID
hotlistID
timestamp

I want all the records from tblHotlist and if records exist in tblESSR, I need those where bugStatusID=300. I've tried several different joins and subqueries, but still can't get the results I need. Once I put the qualifier of bugStatusID=300, I only get the records from tblHotlist where records from tblESSR has a bugStatusID of 300.

failed attempt:

SELECT hl.hotlistID, hl.buildnumber, es.ID, es.notesStatus, es.actionItems
FROM tblhotList hl
     LEFT OUTER JOIN tbl_j_hlbug j ON j.hotlistID = hl.id
     LEFT OUTER JOIN tblESSR es ON j.esrID = es.id
WHERE hl.hotlistStatusID=100 AND hl.loadType='su' AND es.bugStatusID=300

Any help would be appreciated. I've tried different joins and a couple of subqueries, but I always get the same result.

Thanks!

xQbert

You either have to move the limiting criteria to the joins or also look for null values.

WHERE hl.hotlistStatusID=100 AND hl.loadType='su' AND 
(es.bugStatusID=300 or es.bugStatusID is null)

When the outer joins occur, you have to consider null values will exist on records that don't have matching data. As such if you try to limit by these, you will end up excluding the nulls w/o matching data; thereby negating the outer join. Sometimes this is what you want... sometimes it isn't. In this case I think you wanted the nulls and 300.

SELECT hl.hotlistID, hl.buildnumber, es.ID, es.notesStatus, es.actionItems
FROM tblhotList hl
     LEFT OUTER JOIN tbl_j_hlbug j ON j.hotlistID = hl.id
     LEFT OUTER JOIN tblESSR es 
        ON j.esrID = es.id
       AND es.bugStatusID=300
WHERE hl.hotlistStatusID=100 AND hl.loadType='su' 

the Hl where clause doesn't matter as you're getting all records to begin with.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I get all records from one table, even if there are no corresponding records in the JOINed table?

From Dev

How to combine multiple records from a joined table

From Dev

How to get all records from one table and save it on another table by c# in winforms?

From Dev

How to get all records from one table between dates of column in another table

From Dev

Get the repeating records from table only once?

From Dev

Get all records from table - EclipseLink

From Dev

Get all records from azure table storage

From Dev

Get all records from another table with pivot

From Dev

how to get all the records from MySQL table in php web services

From Dev

How to display all records from a table with RedBeanPHP?

From Dev

Delete records From table based on certain criteria

From Dev

How to select all records from one table that do not exist in another table for certain condition in another table?

From Dev

How to select one column clumn from table and all records from relation table [Laravel 5]

From Dev

How to select one column clumn from table and all records from relation table [Laravel 5]

From Dev

how to select all records from one table and some from another table in cakephp 3.6

From Dev

get records without records from another table

From Dev

TSQl Exclude all records from table A based on the given criteria from Table B

From Dev

Get records from third table

From Dev

Get Records from Referenced Table "Minus" All The Records referenced from the Referencing Table

From Java

How to select all records from one table that do not exist in another table?

From Dev

How to select all records from one table that do not exist in another table but return NULL in the record that do not exist

From Dev

SQL: All records from one table, and all records from another, including null

From Dev

Finding duplicate records from table and deleting all but one with latest date

From Dev

Update all mysql records from one table into another

From Dev

Finding duplicate records from table and deleting all but one with latest date

From Dev

SQL Query - select all from one table with matching records in another

From Dev

inserting records from specific columns in one table to another table depending on multiple criteria

From Dev

How to get records from one table which are not in another table's fields (3)

From Dev

How to get 2 id from tow column and get all records from another table

Related Related

  1. 1

    How can I get all records from one table, even if there are no corresponding records in the JOINed table?

  2. 2

    How to combine multiple records from a joined table

  3. 3

    How to get all records from one table and save it on another table by c# in winforms?

  4. 4

    How to get all records from one table between dates of column in another table

  5. 5

    Get the repeating records from table only once?

  6. 6

    Get all records from table - EclipseLink

  7. 7

    Get all records from azure table storage

  8. 8

    Get all records from another table with pivot

  9. 9

    how to get all the records from MySQL table in php web services

  10. 10

    How to display all records from a table with RedBeanPHP?

  11. 11

    Delete records From table based on certain criteria

  12. 12

    How to select all records from one table that do not exist in another table for certain condition in another table?

  13. 13

    How to select one column clumn from table and all records from relation table [Laravel 5]

  14. 14

    How to select one column clumn from table and all records from relation table [Laravel 5]

  15. 15

    how to select all records from one table and some from another table in cakephp 3.6

  16. 16

    get records without records from another table

  17. 17

    TSQl Exclude all records from table A based on the given criteria from Table B

  18. 18

    Get records from third table

  19. 19

    Get Records from Referenced Table "Minus" All The Records referenced from the Referencing Table

  20. 20

    How to select all records from one table that do not exist in another table?

  21. 21

    How to select all records from one table that do not exist in another table but return NULL in the record that do not exist

  22. 22

    SQL: All records from one table, and all records from another, including null

  23. 23

    Finding duplicate records from table and deleting all but one with latest date

  24. 24

    Update all mysql records from one table into another

  25. 25

    Finding duplicate records from table and deleting all but one with latest date

  26. 26

    SQL Query - select all from one table with matching records in another

  27. 27

    inserting records from specific columns in one table to another table depending on multiple criteria

  28. 28

    How to get records from one table which are not in another table's fields (3)

  29. 29

    How to get 2 id from tow column and get all records from another table

HotTag

Archive