Friday, 15 August 2014

sql - How to query Oracle tables using a subquery -



sql - How to query Oracle tables using a subquery -

i trying write single query column in 1 table returns info column. have tried using subquery no luck. prefer find solution using subqueries rather using 'join' still new sql/oracle , can improve understand subqueries...

the first query is:

select ma_gardenermanager.managerid "senior gardener" , ma_gardeners.gardenername , ma_gardenermanager.gardenerid "manages" ma_gardenermanager, ma_gardeners ma_gardeners.gardenerid = ma_gardenermanager.managerid order ma_gardenermanager.managerid , ma_gardenermanager.gardenerid ;

this returns next data:

senior gardener gardenername manages 4 olivia brownish 1 4 olivia brownish 5 5 emily williams 2 5 emily williams 3

the sec query is:

select ma_gardenermanager.gardenerid , ma_gardeners.gardenername "manages" ma_gardeners, ma_gardenermanager ma_gardeners.gardenerid = ma_gardenermanager.gardenerid ;

this returns next data:

gardenerid manages 1 oliver smith 2 jack jones 3 harry taylor 5 emily williams

i trying combine 2 queries above produce this:

senior gardener gardenername manages 4 olivia brownish oliver smith 4 olivia brownish emily williams 5 emily williams jack jones 5 emily williams harry taylor

i've tried maintain getting ora-01427: single-row subquery returns more 1 row:

select ma_gardenermanager.managerid "senior gardener" , ma_gardeners.gardenername , ma_gardenermanager.gardenerid "manages" , (select ma_gardeners.gardenername ma_gardeners, ma_gardenermanager ma_gardeners.gardenerid = ma_gardenermanager.gardenerid) ma_gardenermanager, ma_gardeners ma_gardeners.gardenerid = ma_gardenermanager.managerid ;

is there simple way can combine 2 queries inserting sec query first 1 subquery?

" prefer find solution using subqueries rather using 'join' still new sql/oracle , can improve understand subqueries..."

fnord. sub-query simply complicates simple thing. need reference ma_gardeners table twice in clause. utilize aliases distinguish instances in rest of query:

select gm.managerid "senior gardener" , mgr.gardenername , gar.gardenername "manages" ma_gardenermanager gm , ma_gardeners mgr , ma_gardeners gar mgr.gardenerid = gm.managerid , gar.gardenerid = gm.gardenerid order gm.managerid , gm.gardenerid

;

sql oracle

No comments:

Post a Comment