MySQL INNER/LEFT JOIN on 3 tables, where records in 3rd table might not exist -


i've got problem can't seem figure out after bunch of failed attempts.

i've got 3 tables need join on reporting, , in 3rd table record might not exist. if record in 3rd table doesn't exist, need report null value data comes 3rd table , records match other conditions.

stripped down relevant columns, here table structures:

members - this table holds members register website

| memberid  | insertdate          | | ==========|=====================| | 1         | 2013-08-01 18:18:16 | | 2         | 2013-08-02 18:18:16 | | 3         | 2013-08-03 18:18:16 | | 4         | 2013-08-04 18:18:16 | | 5         | 2013-08-05 18:18:16 | 

registration_steps - this table holds progress of registration processes , whether registration completed or not

| memberid  | completed | | ==========|===========| | 1         | 1         | | 2         | 1         | | 3         | 1         | | 4         | 0         | | 5         | 1         | 

purchases - this table holds, well.. purchases

| memberid  | insertdate          | | ==========|=====================| | 1         | 2013-08-02 18:18:16 | | 1         | 2013-08-03 17:18:16 | | 1         | 2013-08-03 18:18:16 | | 5         | 2013-08-07 18:18:16 | 

this query i've come far:

select `m`.`memberid`,        date(`m`.`insertdate`) `regdate`,        count(`p`.`memberid`) `totaltransactions`,        date(min(`p`.`insertdate`)) `firstpurchasedate`,        date(max(`p`.`insertdate`)) `latestpurchasedate`,        datediff(date(min(`p`.`insertdate`)), date(`m`.`insertdate`)) `daysbetweenregandfirstpurchase`   `db`.`members` `m`        inner join `db`.`registration_steps` `r` on `m`.`memberid` = `r`.`memberid`         inner join `db`.`purchases` `p` on `m`.`memberid` = `p`.`memberid`  `m`.`insertdate` between '2013-07-01 00:00:00' , '2013-07-31 23:59:59'    , `r`.`completed` = 1 group `m`.`memberid` ; 

it shows me want members missing record in table purchases.

here get:

| memberid  | regdate             | totaltransactions | firstpurchasedate   | latestpurchasedate  | daysbetweenregandfirstpurchase | | ==========|=====================|===================|=====================|=====================|================================| | 1         | 2013-08-01 18:18:16 | 3                 | 2013-08-02 18:18:16 | 2013-08-03 18:18:16 | 1                              | | 5         | 2013-08-05 18:18:16 | 1                 | 2013-08-07 18:18:16 | 2013-08-07 18:18:16 | 2                              | 

but need is:

| memberid  | regdate             | totaltransactions | firstpurchasedate   | latestpurchasedate  | daysbetweenregandfirstpurchase | | ==========|=====================|===================|=====================|=====================|================================| | 1         | 2013-08-01 18:18:16 | 3                 | 2013-08-02 18:18:16 | 2013-08-03 18:18:16 | 1                              | | 2         | 2013-08-02 18:18:16 | 0                 | null                | null                | -1                             | | 3         | 2013-08-03 18:18:16 | 0                 | null                | null                | -1                             | | 5         | 2013-08-05 18:18:16 | 1                 | 2013-08-07 18:18:16 | 2013-08-07 18:18:16 | 2                              | 

in order achieve this, tried change second inner join left join, left outer join , put conditions first inner join condition. however, wasn't able desired result. (must admit interupted few long running queries might have been correct(?) though (total count members in real scenario 20k).)

anyone?

thanks in advance!

to result member table need left join other table , add group by member table.

sample

from `db`.`members` `m`        left outer join `db`.`registration_steps` `r` on `m`.`memberid` = `r`.`memberid`         left outer join `db`.`purchases` `p` on `m`.`memberid` = `p`.`memberid` 

left [ outer ]

specifies rows left table not meeting join condition included in result set, , output columns other table set null in addition rows returned inner join.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -