Left Join all lines on the left that do not match the Where clause.

I have the following problem:

I have an account table and entries for an account table. account_id account_name

entry_id
account_idfk
entry_date
entry_amount

Now I want to request all records for all accounts for a certain period. For example. I want all records for all accounts to be from October 2008 to October 2009. If there are no records for this account at all, or there are only records in other time periods for this account, I also want the returned account to be returned.

My current query works if there are no records at all, or there are records for this time period for this account. However, it does not account for accounts that only have entries for other time periods.

SELECT * FROM Account a
         LEFT JOIN Entries e ON e.account_idfk = a.account_id
         WHERE e.entry_date BETWEEN '2009-08-13' AND '2009-08-13'
               OR e.entry_date IS NULL

, where - , .

, , ...

,

+3
1

join:

SELECT
    * 
FROM 
    Account a
    LEFT JOIN Entries e ON 
        e.account_idfk = a.account_id
        AND e.entry_date BETWEEN '2009-08-13' AND '2009-08-13'

join where. join , . left join . where . , - 8/13/09 ( 13/8/09, , ), . join, where.

, inner join , , join where. , , !

+5
source

Source: https://habr.com/ru/post/1715320/


All Articles