Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##these 2 queries are to give you background on the issue
- ##this pulls all users that have records in promos_users that have promos that are promo_type='SALES'
- select u.email, sp.code as sales_promo
- from users u
- left join promos_users pu_sales on (u.id=pu_sales.user_id)
- join promos sp on (sp.code=pu_sales.code and sp.promo_type='SALES')
- where email='zehrtrucking@wigi.us';
- +----------------------+-------------+
- | email | sales_promo |
- +----------------------+-------------+
- | zehrtrucking@wigi.us | 50off |
- +----------------------+-------------+
- ##how the promos_users table looks (can't use code right from this table because it is promo_type agnostic)
- mysql> select * from promos_users where user_id=(select id from users where email='zehrtrucking@wigi.us');
- +-----+---------+--------+---------------------+
- | id | user_id | code | ts |
- +-----+---------+--------+---------------------+
- | 209 | 36222 | 50off | 2008-04-18 11:54:44 |
- | 329 | 36222 | Bronze | 2008-04-28 15:38:06 |
- +-----+---------+--------+---------------------+
- ##this following solution looks successful, that is indeed the output i need for a company that has a sales and saves promo, but i added in demo@loadmax.com in the where clause, no record matches!
- mysql> select u.email, sp.code as sales_promo, svp.code as saves_promo
- from users u
- left join promos_users pu_sales on (u.id=pu_sales.user_id)
- join promos sp on (sp.code=pu_sales.code and sp.promo_type='SALES')
- left join promos_users pu_saves on (u.id=pu_saves.user_id)
- join promos svp on (svp.code=pu_saves.code and svp.promo_type='SAVES')
- where email in ('zehrtrucking@wigi.us','demo@loadmax.com');
- +----------------------+-------------+-------------+
- | email | sales_promo | saves_promo |
- +----------------------+-------------+-------------+
- | zehrtrucking@wigi.us | 50off | Bronze |
- +----------------------+-------------+-------------+
- 1 row in set (0.00 sec)
- ##but if we the joins to promos left joins ... i get the records , but look how messed up the results are.. now i'm getting duplicates for the mail column
- mysql> select u.email, sp.code as sales_promo, svp.code as saves_promo
- from users u
- left join promos_users pu_sales on (u.id=pu_sales.user_id)
- left join promos sp on (sp.code=pu_sales.code and sp.promo_type='SALES')
- left join promos_users pu_saves on (u.id=pu_saves.user_id)
- left join promos svp on (svp.code=pu_saves.code and svp.promo_type='SAVES')
- where email in ('zehrtrucking@wigi.us','demo@loadmax.com');
- +----------------------+-------------+-------------+
- | email | sales_promo | saves_promo |
- +----------------------+-------------+-------------+
- | demo@loadmax.com | NULL | NULL |
- | zehrtrucking@wigi.us | 50off | NULL |
- | zehrtrucking@wigi.us | 50off | Bronze |
- | zehrtrucking@wigi.us | NULL | NULL |
- | zehrtrucking@wigi.us | NULL | Bronze |
- +----------------------+-------------+-------------+
- 4 rows in set (0.00 sec)
- ##here is the output i need
- +----------------------+-------------+-------------+
- | email | sales_promo | saves_promo |
- +----------------------+-------------+-------------+
- | demo@loadmax.com | NULL | NULL |
- | zehrtrucking@wigi.us | 50off | Bronze |
- +----------------------+-------------+-------------+
- ##here is the solution .. inspired by gnari (produces above output)
- select u.email, sp.code as sales_promo, svp.code as saves_promo
- from users u
- left join (promos_users pu_sales join promos sp on (sp.code=pu_sales.code and sp.promo_type='SALES')) on (pu_sales.user_id=u.id)
- left join (promos_users pu_saves join promos svp on (svp.code=pu_saves.code and svp.promo_type='SAVES')) on (pu_saves.user_id=u.id)
- where email in ('zehrtrucking@wigi.us','demo@loadmax.com');
Add Comment
Please, Sign In to add comment