Re: Help with JOIN query
| From: | Graham Cossey | Date: | Thu, 06 Mar 2008 19:09:15 +0000 |
| Subject: | Re: Help with JOIN query | ||
| References: | 1 2 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-44702@lists.php.net to get a copy of this message | ||
On Thu, Mar 6, 2008 at 6:54 PM, Krister Karlström
<krister.karlstrom@arcada.fi> wrote:
> Hi!
>
>
> Graham Cossey wrote:
>
> > TableA
> > record_id
> > product_ref
> >
> > TableB
> > timestamp
> > record_id
> > action
> >
> > I want to create a SELECT that joins these 2 tables where the JOIN to
> > TableB only returns the most recent entry by timestamp.
>
> For instance, to select all columns:
>
> select * from TableA
> join TableB on TableA.record_id = TableB.record_id
> order by timestamp desc
> limit 1
>
> So you just join everything, then order by time in descening order and
> then just returning the first record = the newest record in the
> database. If you don't want all columns, then simply replace the star
> with the names of the columns you want.
>
> I hope that this is what you wanted the query to do.. :)
>
I was hoping to avoid joining everything as there can be many entries
in TableB for each record in TableA.
Also wouldn't your query only return one record? I need to return all
records from TableA with the latest action from TableB as well.
Graham
--
Graham