MYSQL Theta Join to empty table?

MYSQL Theta Join to empty table?

Postby cccp » Wed Sep 03, 2008 12:02 pm

I am having problems with some SQL I am writing. I have a table of rooms and a table of recordings and I want to return the last recording ID in each room. This is no big deal except when the system is initialized and there are no records in the recordings table. The query returns an empty result set. I have read about THETA JOINS and it seems to be what I am looking for but I cant find any examples of how to put this kind of SQL statement together in MYSQL.

Thanks

User avatar
cccp
Newbie
Newbie
 
Posts: 35
Joined: Mon Apr 30, 2007 10:31 am

Re: MYSQL Theta Join to empty table?

Postby Darwin » Wed Sep 03, 2008 12:04 pm

Please post your SQL to the thread.
User avatar
Darwin
Full Member
Full Member
 
Posts: 111
Joined: Mon Oct 09, 2006 11:16 am
Location: Bradenton, FL

Re: MYSQL Theta Join to empty table?

Postby cccp » Thu Sep 04, 2008 12:28 pm

Here is my sql query that dosent work.

Code: Select all
SELECT    rooms.id,
          MAX(recs.id) AS MAX_recs_id
FROM rooms
        INNER JOIN recs
              ON rooms.id = recs.room_id
GROUP BY rooms.id
User avatar
cccp
Newbie
Newbie
 
Posts: 35
Joined: Mon Apr 30, 2007 10:31 am

Re: MYSQL Theta Join to empty table?

Postby Darwin » Thu Sep 04, 2008 12:32 pm

Try this
Code: Select all
    SELECT    rooms.id,
              MAX(recs.id) AS MAX_recs_id
    FROM rooms
            LEFT OUTER JOIN recs
                  ON rooms.id = recs.room_id
    GROUP BY rooms.id
User avatar
Darwin
Full Member
Full Member
 
Posts: 111
Joined: Mon Oct 09, 2006 11:16 am
Location: Bradenton, FL

Re: MYSQL Theta Join to empty table?

Postby cccp » Thu Sep 04, 2008 12:41 pm

That works but it is returning <NULL> values if there are no records. How can I get it to return zero if there are no records?
User avatar
cccp
Newbie
Newbie
 
Posts: 35
Joined: Mon Apr 30, 2007 10:31 am

Re: MYSQL Theta Join to empty table?

Postby Darwin » Thu Sep 04, 2008 12:43 pm

Use this

Code: Select all
 
    SELECT    rooms.id,
              COALESCE(MAX(recs.id),0) AS MAX_recs_id
    FROM rooms
            INNER JOIN recs
                  ON rooms.id = recs.room_id
    GROUP BY rooms.id

User avatar
Darwin
Full Member
Full Member
 
Posts: 111
Joined: Mon Oct 09, 2006 11:16 am
Location: Bradenton, FL


Return to MYSQL

Who is online

Users browsing this forum: No registered users and 0 guests

cron