| | |
Join on a view without the view
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Apr 2009
Posts: 4
Reputation:
Solved Threads: 0
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
Here is the query that is using that view on the right side.
So how do I combine them into one statement? I tried creating a subquery but I couldnt get it working correctly.
TIA.
Here is the current view's SQL statement ( without the create view etc)
VIEW NAME: vw_2009
MySQL Syntax (Toggle Plain Text)
SELECT fundinfo.fk_budget_ID, fundinfo.fk_tiertwo_ID, Sum(fundlink.fundlink_amt) FROM (fundinfo LEFT JOIN fundlink on((fundinfo.pk_fund_ID = fundlink.fk_fund_ID`))) WHERE ((fundlink.date_create <= '2009-03-15') AND (fundlink.date_create >= '2009-01-01') AND (fundinfo.fundinfo_budg_yr = '2009')) GROUP BY fundinfo.fk_tiertwo_ID
Here is the query that is using that view on the right side.
MySQL Syntax (Toggle Plain Text)
SELECT budget.budget_name, vw_2009.Sum(fundlink.fundlink_amt) FROM budget 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.
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:
Hope this helps,
darkagn

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)
SELECT fundinfo.fk_budget_ID, fundinfo.fk_tiertwo_ID, Sum(fundlink.fundlink_amt), budget.budget_name FROM fundinfo LEFT JOIN fundlink ON fundinfo.pk_fund_ID = fundlink.fk_fund_ID LEFT JOIN budget ON budget.pk_budget_ID = fundinfo.fk_budget_ID WHERE fundlink.date_create <= '2009-03-15' AND fundlink.date_create >= '2009-01-01' AND fundinfo.fundinfo_budg_yr = '2009' GROUP BY fundinfo.fk_tiertwo_ID, budget.budget_name
Hope this helps,
darkagn
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. •
•
Join Date: Apr 2009
Posts: 4
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jun 2008
Posts: 79
Reputation:
Solved Threads: 8
Try this
MySQL Syntax (Toggle Plain Text)
SELECT budget.budget_name, total.fundlink.fundlink_amt FROM budget LEFT JOIN ( SELECT fundinfo.fk_budget_ID, fundinfo.fk_tiertwo_ID, Sum(fundlink.fundlink_amt) FROM (fundinfo LEFT JOIN fundlink on((fundinfo.pk_fund_ID = fundlink.fk_fund_ID`))) WHERE ((fundlink.date_create <= '2009-03-15') AND (fundlink.date_create >= '2009-01-01') AND (fundinfo.fundinfo_budg_yr = '2009')) GROUP BY fundinfo.fk_tiertwo_ID )total ON budget.pk_budget_ID = total.fk_budget_ID
Last edited by varmadba; Apr 29th, 2009 at 8:43 am.
:- Varma
We are Happy to inform launch of a new site with loads of database related information Site offers wide range of functionality Forums,Blogs,Articles,Editorials and much more
http://www.sqllibrarian.info/
We are Happy to inform launch of a new site with loads of database related information Site offers wide range of functionality Forums,Blogs,Articles,Editorials and much more
http://www.sqllibrarian.info/
![]() |
Similar Threads
- bank system problem !!!! (C++ programming) (C++)
- How to always view answers on Experts Exchange (Geeks' Lounge)
- Computed column in view VS trigger on update (MS SQL)
- image maker (Community Introductions)
- please review my forum (Website Reviews)
- call resultset in join query (JSP)
- Invision Power Board mysql error when trying to view newly created forums (PHP)
Other Threads in the MySQL Forum
- Previous Thread: Conditional condition
- Next Thread: MySQLdb interface problem
| Thread Tools | Search this Thread |
agplv3 alfresco api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright count court crm database design developer development distinct drupal dui ec2 email enterprise eudora facebook form foss gnu government gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier join journalism keyword keywords kickfire laptop law legal license licensing linux maintenance managing mariadb matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource oracle penelope php query referencedesign reorderingcolumns resultset saas select sharepoint simpledb sourcecode spotify sql sugarcrm syntax techsupport thunderbird transparency virtualization





