tags:

views:

20

answers:

2
SELECT S.CLIENT,S.IP_DOMAIN as IP, IFNULL(K.DATE, DATE '0000-00-00') AS RecentDate
      FROM PLD_SERVERS AS S JOIN PLD_SEARCHES AS K ON S.ID = K.SERVER_ID

This query will produce as many results as entries in the PLD_SEARCHES. For example:

I have 3 entries in PLD_SERVERS and 18 entries in PLD_SEARCHES. The output of this query will be 18 but i need it to be 3 (as the number of PLD_SERVERS entries) with the recent date as a join field from PLD_SEARCHES.

A: 

Try using a left join:

   SELECT  S.CLIENT,S.IP_DOMAIN as IP, IFNULL(K.DATE, DATE '0000-00-00') AS RecentDate
     FROM  PLD_SERVERS AS S 
LEFT JOIN  PLD_SEARCHES AS K ON S.ID = K.SERVER

The problem probably originates from an incorrect JOIN type for this situation. Check the different types of join:

  1. http://en.wikipedia.org/wiki/Join_%28SQL%29
  2. http://www.wellho.net/mouth/158_MySQL-LEFT-JOIN-and-RIGHT-JOIN-INNER-JOIN-and-OUTER-JOIN.html
Phillykins
true it is an incorrect join but i can't figure out how.
+1  A: 

How about something like:

SELECT S.CLIENT,S.IP_DOMAIN as IP
    , IFNULL(
        (
        Select Max(K2.DATE)
        From PLD_SEARCHES AS K1 
        Where S.ID = K1.SERVER_ID
        ), DATE, '0000-00-00') AS RecentDate
FROM PLD_SERVERS AS S 
Where Exists(   
            Select 1
            From PLD_SEARCHES AS K1 
            Where S.ID = K1.SERVER_ID
            )

Here I'm using the Exists function to determine which rows to show and then I use a second subquery to find the last date.

Thomas
not working...i need to select all from PLD_SERVERS and then join a column with the most recent date from PLD_SEARCHES if the PLD_SERVERS.ID = PLD_SEARCHES.SERVER_ID holds
got it working thanks. I needed to modify some arguments and a comma :)
Where was my comma out of place so I can update the answer?
Thomas