David W Noon wrote:
A nested query

Select
   orders.*,
   (SELECT MAX(ol_timestamp) FROM orders_log where orders_log.o_id =
orders.oid)
    
>From orders

That won't give the desired results. I don't think the SQL parser will
even accept it.
  
Yes this is a  valid SQL statement use similar ones a great deal.  The problem with nested queries is they can only return 1 record per match.  Another problem can be performance it has to run for every record in order tables and it occurs pre filter conditions
  
Still another option is using a join

Select
   orders.*, ol_timestamp
   From orders
   left join (SELECT MAX(ol_timestamp), o_id FROM orders_log group by
o_id) as JoinQuery on JoinQuery.o_id = orders.o_id
    

That won't give the desired results either. If you change "left" to
"inner" you will be closer though.

Both of your queries will retrieve the entire orders table with a
timestamp of some sort from the orders_log table.
  

I read the question as Gary wanted the entire orders table with the greatest timestamp from the logs table....

Quoting Gary
"How can I select all from orders and the last (latest) entry from the 
orders_log?"




Reply via email to