?
Solved

Left Join with condition in joined table

Posted on 2007-10-17
2
Medium Priority
?
1,068 Views
Last Modified: 2008-01-09
Hi Experts:

I have tables which I am joining like so:

select
  site.site_id,
  site.site_name,
  sum(distinct(test.`nr_of_part`)) as total_part,
  site_type.site_type_name
                    
                    
    from site_type, site
   
     left outer join site_cases
      on site.`site_id` = site_cases.`site_id`
     
      left outer join test
        on site.`site_id` = test.`site_id`
   
   
    where site.site_type_id = site_type.site_type_id
   
            
            
        and UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(test.`test_end`) <= '31536000'
                                          
                        group by
                          site.site_id

This statement works as expected (I shortened it a bit...). Except for one thing which I am trying to get around:
When I leave out the "UNIX_TIMESTAMP..." condition, I get all sites regardless of if they have any tests done or not. If I put in the condition I am not getting sites which have no tests anymore...

I underrstand that this makes sense in such a way that if I am requesting only sites which have tests in a certain timeframe, that I won't get those who do not have studies.

Here is my question: Is there a way to get all Sites (regardless if they habe done a test or not) whilst only using the timeframe restriction for calculating the sum()?

Sorry if this is a bit confusing. Let me know if you have any questions in regards to this.

kracklt :-)

BTW: I am using MySQL 5.1
0
Comment
Question by:kracklt
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 20092472
move the condition of the left joined table to the join condition itself:

select
  site.site_id,
  site.site_name,
  sum(distinct(test.`nr_of_part`)) as total_part,
  site_type.site_type_name
                     
                   
    from site_type, site
   
     left outer join site_cases
      on site.`site_id` = site_cases.`site_id`
     
      left outer join test
        on site.`site_id` = test.`site_id`
        and UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(test.`test_end`) <= '31536000'
   
   
    where site.site_type_id = site_type.site_type_id
           
                                         
                        group by
                          site.site_id
0
 

Author Comment

by:kracklt
ID: 20093125
Hi Angel:

Perfect. I did not know thet the left join statement can be extendend :-)

Thanks a lot!

Kracklt
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month16 days, 6 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question