Sort Results one Table Based on Row of Another

I’ve got two tables:

clients:
id, first, last

contactlog:
cid, client, date, notes

now I’m echoing out the list of clients, like so:

Last Contact First Name Last Name
11/26/2010 David Wortham
04/15/2010 Wendy Talon
06/22/2010 Allison Morey

by quering the clients database, then a nested query to pull the latest contact from the log to get the ‘last contacted’ date.

QUESTION: how do i resort this list to order it by date of last contact?

i.e.
04/15/2010 Wendy Talon
06/22/2010 Allison Morey
11/26/2010 David Wortham

I would normally query the date with sub query like this.

(Presuming client is the foreign key in contactlog table)


SELECT
	c.first,
	c.last,
	(SELECT cl.date FROM contactlog AS cl WHERE cl.client=c.id ORDER BY cl.date DESC LIMIT 1) AS logdate
FROM
	clients AS c
ORDER BY 
	logdate ASC;

Hope to see better queries.

Rajug,

THANK YOU so much! That did it!
Now I just get to figure out how to make each column sortable by clicking on the column headers. Thanks again.

b.

well, since you asked …

SELECT cl.latest
     , c.first
     , c.last
  FROM clients AS c
LEFT OUTER
  JOIN ( SELECT client
              , MAX(`date`) AS latest
           FROM contactlog 
         GROUP
             BY client ) AS cl
    ON cl.client = c.id 
ORDER 
    BY latest ASC

:slight_smile:

Thanks rudy, I was about to put your name before I ask there. You have always best solutions :slight_smile:

In rajugs’ query he’s ordering by date desc, date might not be indexed.
In r937 - result is obtained by using aggregate function, again on a non-indexed column.
I assume date isn’t indexed.
If the “cid” column is auto_increment here, why not just order by id desc limit 1? No aggregate function involved, no additional index needed.
Thing is, since it isn’t mentioned explicitly, date of last contact will have the highest cid column unless there’s a certain application-level feature preventing it from being so.

you’re right, blue, that there’s a possibility that date might not be indexed

the easy solution is to add an index, and an index on (cid,date) would be a covering index for that table in this query

what’s not right, in my opinion, is to rely on the auto_increment number for sequence

that’s a train wreck waiting to happen :slight_smile:

auto_increments should be used for their uniqueness and nothing more

It’s not that I’m disagreeing on the use of auto_increments, but there is a possibility that every new entry for the client contact contains date that’s later than the previous entry - hence, there’s nothing preventing us from using cid as means of obtaining the latest date :slight_smile:
Also, if it is such feature, we don’t need the composite index of cid and date.
If the feature isn’t such, then I’d definitely go with r937’s query after making the
suggested index change.

hence? i don’t understand “hence” :wink:

say you have a large table, and you insert into it daily

then it is discovered that you missed one day’s inserts, and it’s now 3 days later

all you have to do is insert the missing data

… unless, of course, you’ve chained yourself to the ridiculous notion that higher auto_increment numbers mean later dates – in that case, you will first have to delete all the stuff inserted after the missing day’s data, then add them all back in the right sequence

train wreck waiting to happen

:slight_smile:

then it is discovered that you missed one day’s inserts, and it’s now 3 days later

And how does exactly this miss occur? Magically out of the blue and then the system continues working? :slight_smile:
And who discovers the error 3 days later?

If you enter an entry each time you contacted the client, the date of contact will be the latest one. Consequently, for that client, the auto_increment will be the largest one too, in the table that stores the time of contact. We can go over catastrophic disaster plan here, what if this and that but I’m still not wrong and if something crashes in such way that even application logic fails - there’s a much bigger problem at hand then.
If the application logic is as the one I described, why not use the shortcut and you avoid indexing another field? Yes, there are potential errors but I’d really like to see them happening before saying I’m wrong :>

i’m going to excuse myself from further discussion of this topic

to find the latest entry, use a date/datetime column

full stop

:slight_smile:

You guys are talking past each other. Blue is correct if the chronological age of the records independent of their insertion sequence isn’t important, but if it is important an insertion timestamp of some sort should be kept, which case r937 is right.

In short, you each mean something slightly different by the phrase “latest entry.”

I’d go with the timestamp approach like r937 though - it’s rarely useful to know the actual insertion sequence of the records, importing historical data being a key example of how trying to use auto index as a chronological record can go horribly wrong.