|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
sql query
Hi,
I have two tables like this: Table-1 Route|srcid|destid A 1 2 B 3 4 Table-2 id | Name 1 ValX 2 ValY 3 ValM 4 ValN Can any one help me write query which returns this Route|Srcid|destid|SrcName(Name)|DestName(Name) A 1 2 ValX ValY B 3 4 ValM ValN Thanks in advance bskr |
|
#2
|
|||
|
|||
|
Code:
select route,scrid,
(select name from t2
where id = t1.scrid) as srcName,
destid,
(select name from t2
where id = t1.destid) as DestName
from t1
|
|
#3
|
|||
|
|||
|
Thank you. I'll try that.
bskr |
|
#4
|
||||
|
||||
|
If you want to do it in one query (or you are forced to, like in MySQL < 4.0.9), try:
SELECT t1.Route, t1.srcid, t1.destid, alias1.Name AS Src, alias2.Name AS Dest FROM t1, t2 AS alias1, t2 AS alias2 WHERE t1.srcid = alias1.id AND t1.destid = alias2.id
__________________
My blog about OpenSource Databases PDF tutorials about OSS databases, DBMonster ... Please contribute to Open Source Development, fill bug reports!!! Developer Shed eSupport Commented my.ini/my.cnf (PLEASE ADD YOUR OWN CONFIG TRICK) An introduction to database normalization Natural or Surrogate key Custom ordering for your results Correlated and uncorrelated subqueries Don't turn your outer joins into inner joins |
![]() |
| Viewing: Dev Shed Forums > Databases > Database Management > sql query |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|