View Single Post

   
  #5 (permalink)  
Old 03-28-2008, 04:40 AM
Paul Lautman
 
Posts: n/a
Default Re: need help on sql

Junkone wrote:
> On Mar 20, 10:04 pm, Michael Austin <maus...@firstdbasource.com>
> wrote:
>> Junkone wrote:
>> > i have 3 tables which are linked to 1 another using foreigh key-
>> > Trades, executions, rawdatas with id as primary key
>> > Trades can have 1 or more executions
>> > Executions must have only 1 rawdatas

>>
>> > The structure is
>> > Rawdatas cols
>> > 1. id
>> > 2. Date
>> > 3. col2
>> > 4. col3

>>
>> > Executions.
>> > 1. id
>> > 2. EntryOrExit
>> > 3. col2
>> > 4. col3

>>
>> > Trades
>> > 1. id
>> > 2. col1
>> > 3. col2
>> > 4. col3

>>
>> > I want to build a query which returns all the trades and return the
>> > value of RawDatas.date where Executions.EntryorExit="Entry"
>> > i need some help.

>>
>> > seede

>>
>> Please post what you have tried. We can help, but would like to see
>> where you are going wrong.. and if this is homework you should be
>> figuring this out for yourself and not having others do it for
>> you...(I doubt any real trading company would hire someone to do
>> this in MySQL - especially someone who shows very little knowledge
>> of SQL for something this simple...)- Hide quoted text -
>>
>> - Show quoted text -

>
> select r.created_at,t.* from trades t, executions e, rawdatas r where
> r.execution_id=e.id and e.trade_id=t.id
> and e.entryorexit='ENTRY'
>
> The # of rows in Trades=353. The rows returned by this sql is 409
> because there are 1:n relationshiop between Trades and Executions. I
> just wanted the values form the first record from executions that is
> related to Trades. I would really appreciate some help here because i
> can go only so far.
> PS: This is not homework. I am a starter programmer doing some work
> for a smalltime company.


The answer is here:
http://dev.mysql.com/doc/refman/5.0/...group-row.html
you want to base your query on the third example, the one with the LEFT
JOIN.


Reply With Quote