joining 3 tables

hello to all

any idea how to code joining 3 tables?

tnx

Could you be a bit more specific?

I presume that you mean an SQL Join. There are at least a couple of ways to do this, but would need to understand more on what you are trying to accomplish.

Moved to General Database section, because it’s probably a SQL Join that the TS is talking about.

yes, i use MSSQL because our company use the microsoft product. such as Great Plains.

i have one query that all the data i want is in the 3 different tables
ex:

tbl1:
emp_id
emp_name

tbl2:
emp_id
previous_year

tbl3:
emp_id
current_year

and ADD the value of tbl2.previous_year and tbl3.current_year

my problem is… how to code that one in PHP :D

SELECT a.emp_id, a.emp_name, b.previous_year, c.current_year
FROM tbl1 a, tbl2 b, tbl3 c
WHERE a.emp_id = b.emp_id
AND b.emp_id = c.emp_id

may i ask what the difference between this two queries?

SELECT PM00200.*, GL30000.*, GL20000.* FROM PM00200 INNER JOIN GL30000 ON GL30000.ORMSTRID = PM00200.VENDORID INNER JOIN GL20000 ON GL20000.ORMSTRID = GL30000.ORMSTRID WHERE GL20000.ACTINDX='$lndex'

and

SELECT a.VENDORID, a.VENDNAME, b.ORMSTRID, c.ORMSTRID FROM PM00200 a, GL20000 b, GL30000 c WHERE a.VENDORID = b.ORMSTRID AND b.ORMSTRID = c.ORMSTRID AND GL20000.ACTINDX='$lndex'

hello, i have a problem with this query…

SELECT a.VENDORID, a.VENDNAME, b.ORMSTRID, b.ACTINDX, c.ACTINDX, c.ORMSTRID, sum(b.CRDTAMNT) curcre, sum(b.DEBITAMT) curdeb, sum(c.CRDTAMNT) precre, sum(c.DEBITAMT) predeb FROM PM00200 a, GL20000 b, GL30000 c WHERE b.ORMSTRID = a.VENDORID AND c.ORMSTRID = a.VENDORID AND b.ACTINDX=$lndex AND c.ACTINDX=$lndex GROUP BY a.VENDORID, a.VENDNAME, b.ORMSTRID, b.ACTINDX, c.ACTINDX, c.ORMSTRID

in table GL30000 i have 3 records in the same id and table GL20000 i have 57 records in the same id also.
after the query the return of my table GL30000 will be 57 also.
if my GL20000 will return 60, the GL30000 will return 60 also. were the original record in my table GL30000 is only 3.

any idea how to fix that one?
tnx in advance

You cannot have multiple SUM statements in the query. SUM is an agregate function. This query won’t work. You have to do it in smaller steps as opposed to doing one big query.

There are so many issues I see that would prevent this query from running, that I am not sure where to begin.

Sponsor our Newsletter | Privacy Policy | Terms of Service