943,852 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Unsolved
  • Views: 1310
  • MySQL RSS
Apr 21st, 2009
0

Join on a view without the view

Expand Post »
Hi I was wondering what the syntax would be to have a left join from one table to a resulting query without the view. Currently I use a view and it works fine but I was wondering how to achieve this without creating the view.

Here is the current view's SQL statement ( without the create view etc)

VIEW NAME: vw_2009
MySQL Syntax (Toggle Plain Text)
  1. SELECT
  2. fundinfo.fk_budget_ID,
  3. fundinfo.fk_tiertwo_ID,
  4. Sum(fundlink.fundlink_amt)
  5. FROM (fundinfo LEFT JOIN fundlink on((fundinfo.pk_fund_ID = fundlink.fk_fund_ID`)))
  6. WHERE ((fundlink.date_create <= '2009-03-15') AND (fundlink.date_create >= '2009-01-01') AND (fundinfo.fundinfo_budg_yr = '2009'))
  7. GROUP BY fundinfo.fk_tiertwo_ID



Here is the query that is using that view on the right side.

MySQL Syntax (Toggle Plain Text)
  1. SELECT
  2. budget.budget_name,
  3. vw_2009.Sum(fundlink.fundlink_amt)
  4. FROM
  5. budget
  6. LEFT JOIN vw_2009 ON budget.pk_budget_ID = vw_2009.fk_budget_ID


So how do I combine them into one statement? I tried creating a subquery but I couldnt get it working correctly.

TIA.
Last edited by mr_vodka; Apr 21st, 2009 at 2:35 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mr_vodka is offline Offline
4 posts
since Apr 2009
Apr 21st, 2009
0

Re: Join on a view without the view

Hi mr_vodka and welcome to DaniWeb

Love the username btw!

If all you want from the budget table is the budget_name field, you could just try this:
mysql Syntax (Toggle Plain Text)
  1. SELECT
  2. fundinfo.fk_budget_ID,
  3. fundinfo.fk_tiertwo_ID,
  4. Sum(fundlink.fundlink_amt),
  5. budget.budget_name
  6. FROM fundinfo
  7. LEFT JOIN fundlink
  8. ON fundinfo.pk_fund_ID = fundlink.fk_fund_ID
  9. LEFT JOIN budget
  10. ON budget.pk_budget_ID = fundinfo.fk_budget_ID
  11. WHERE fundlink.date_create <= '2009-03-15' AND fundlink.date_create >= '2009-01-01' AND fundinfo.fundinfo_budg_yr = '2009'
  12. GROUP BY fundinfo.fk_tiertwo_ID, budget.budget_name

Hope this helps,
darkagn
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Apr 22nd, 2009
0

Re: Join on a view without the view

Click to Expand / Collapse  Quote originally posted by darkagn ...
Hi mr_vodka and welcome to DaniWeb
Thanks for the welcome and assistance darkagn.

I tried your suggestion but its a no go. It still will not return all the records in the budget table.

I even tried to switch the order of the joins by putting the budget to Fund LEFTJOIN first but it didnt help.


Currently, when I am using the simple LEFTJOIN with the view it will result in 15 records. If I use the statement that you provided, it will only show 3.

I figure that it was a matter or order or parathensis but I just seem to get it correct. I dont want to have to create a temp view just to achieve this. I am hoping that there is a way to do it with one SQL statement.

Thanks again.
Last edited by mr_vodka; Apr 22nd, 2009 at 11:38 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mr_vodka is offline Offline
4 posts
since Apr 2009
Apr 24th, 2009
0

Re: Join on a view without the view

Bump... Anyone?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mr_vodka is offline Offline
4 posts
since Apr 2009
Apr 29th, 2009
0

Re: Join on a view without the view

Try this

MySQL Syntax (Toggle Plain Text)
  1. SELECT
  2. budget.budget_name,
  3. total.fundlink.fundlink_amt
  4. FROM
  5. budget
  6. LEFT JOIN
  7. (
  8. SELECT
  9. fundinfo.fk_budget_ID,
  10. fundinfo.fk_tiertwo_ID,
  11. Sum(fundlink.fundlink_amt)
  12. FROM (fundinfo LEFT JOIN fundlink on((fundinfo.pk_fund_ID = fundlink.fk_fund_ID`)))
  13. WHERE ((fundlink.date_create <= '2009-03-15') AND (fundlink.date_create >= '2009-01-01') AND (fundinfo.fundinfo_budg_yr = '2009'))
  14. GROUP BY fundinfo.fk_tiertwo_ID
  15. )total
  16. ON budget.pk_budget_ID = total.fk_budget_ID
Last edited by varmadba; Apr 29th, 2009 at 8:43 am.
Reputation Points: 22
Solved Threads: 9
Junior Poster in Training
varmadba is offline Offline
83 posts
since Jun 2008
May 28th, 2009
0

Re: Join on a view without the view

Great. Much appreciated. I couldnt get passed the setting of the "total" alias. Thanks for helping me out.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mr_vodka is offline Offline
4 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: Conditional condition
Next Thread in MySQL Forum Timeline: MySQLdb interface problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC