Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem w- aliases and multiple joins

Status
Not open for further replies.

admoore

IS-IT--Management
May 17, 2002
224
US
I am having problems with a multiple join query...

While this query produces an erroneous result due to no aliases defining multiple joins, it does run:
Code:
           SELECT users_vins.vin, users_vins.reg_date,  Sum( points_service.points ) AS service_reward, Sum( points_sales.points ) AS sales_reward 
                  FROM users_vins
                  INNER JOIN points_service ON users_vins.acct_no = points_service.acct_no 
                  INNER JOIN points_sales ON users_vins.acct_no = points_sales.acct_no
		  GROUP BY users_vins.acct_no, vin
                  ORDER BY users_vins.acct_no ASC

This query attempts to solve the erroneous result with aliases; but, produces an error: [COLOR=red yellow]#1109 - Unknown table 'points_service' in field list[/color]
Code:
SELECT users_vins.vin, users_vins.reg_date,  Sum( points_service.points ) AS service_reward, Sum( points_sales.points ) AS sales_reward 
                  FROM users_vins
                  INNER JOIN points_service AS service ON users_vins.acct_no = points_service.acct_no 
                  INNER JOIN points_sales AS sales ON users_vins.acct_no = points_sales.acct_no
		  GROUP BY users_vins.acct_no, vin
                  ORDER BY users_vins.acct_no ASC

Can anyone help with my syntax please?

TIA,

Allen M.
 
While this query produces an erroneous result due to no aliases defining multiple joins ...
What do you mean? aliases are not necessary and overly use of them only make queries illegible.

But if you use them, you should use them consistently. Once you have given a table an alias, call it by the alias, also in its own JOIN clause:
Code:
INNER JOIN points_service AS service ON users_vins.acct_no = [highlight]service[/highlight].acct_no

The reason is that you can join to the same table twice, one time non-aliased and one time aliased.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
After correcting my aliases like this:
Code:
SELECT users_vins.vin, users_vins.reg_date, Sum( service.points ) AS service_reward, Sum( sales.points ) AS sales_reward
FROM users_vins
INNER JOIN points_service AS service ON users_vins.acct_no = service.acct_no
INNER JOIN points_sales AS sales ON users_vins.acct_no = sales.acct_no
GROUP BY users_vins.acct_no, vin
ORDER BY users_vins.acct_no ASC

I no longer have the error; but the result is still erroneous with sums far in excess of those that actually exist, presumably due to the multiple joins...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top