Link to home
Start Free TrialLog in
Avatar of azsd
azsd

asked on

how to use group by in mulity table query?

I have two table that named table_user and table_area,
in table_user there some user records:
=========================
username,areacode,sex
=========================
azsd,0376,1
fake,0377,0
chome,0376,1
user3,0376,1
=========================

in table_area stores areacodes and their areaname
=========================
areacode,areaname
=========================
0376,xinyang
0377,nanyang
=========================

I want to make an query return back records like that:
=========================
username,areaname,sex
=========================
azsd,xinyang,1
fake,nanyang,0
chome,xinyang,1
user3,xinyang,1
=========================

which format sql phras that I would use?
I use that text and sql anlays can't pass though grammar check:
select "table_user".username, "table_area".areaname,"table_user".sex from "table_user","table_area","table_user" group by "table_user".areacode;
sure i don't think it can work.
the help files with oracle 9i have only few graphic,no more info....

thanks you read this question post by a poorman poor in english
Avatar of pratikroy
pratikroy

You have a simple requirement :

Select a.username, b.areaname, a.sex
From  table_user a, table_area b
Where a.areacode = b.areacode;

Hope this helps!
Avatar of azsd

ASKER

yes it works,
unluckly the where statements occured an little things in my envorenment:
i have an user record

userx,0378,1

it will discarded and only 4 rows selected....
I want have these results

azsd,xinyang,1
fake,nanyang,0
chome,xinyang,1
user3,xinyang,1
userx,0378,1

how can i redirect the areacode that no exists in table_area as areaname?
Avatar of azsd

ASKER

I have this idea:

Select a.username, b.areaname, a.sex
From  table_user a, table_area b
Where a.areacode = b.areacode
union
select username,areacode as areaname,sex
from table_user
where areacode not in (select areacode from table_area);

but i think "not in (select)" will be slowest in oracle commands
ASKER CERTIFIED SOLUTION
Avatar of mottor
mottor
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of azsd

ASKER

thanks pratikroy and mottor,
tested fine in my server~~
You are welcome azsd. Glad I could be of some help to you !