Author |
Topic |
krjaga
Starting Member
3 Posts |
Posted - 2005-03-15 : 12:28:58
|
Hi,I am having two Tables. In Which i have to join these tables.In fact if we join the tables, the result set from two table which are matching will be concatenated in a single row.But, here i need two get results in a separate rows..is it possible ???Ex : Table 1:Id Name 1 Jack2 PhilipTable 2 :Id Address1 London2 GlascowResult will be :(joining by id)1 Jack London2 Philip GlascowBut i need the Answer as1 Jack2 Philip1 London2 Glascow...U can say that we can easily do this by Union..but i need to join with a condition.Please Help.Thanks & Regards,Jagadhees |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-03-15 : 12:32:26
|
what condition do you need? There is no join at all in your sample, just a regular UNION. you need to give us more sample data or some more information about why a regular UNION will not work for you.- Jeff |
 |
|
krjaga
Starting Member
3 Posts |
Posted - 2005-03-15 : 12:47:39
|
Hi,the Condition for the join is select * from table1 join table2 On table1.id = table2.id...this kind of condition..but i should give union result..pls help..The Requirement is, i have two tables, For each row in the First table, i have to fetch a row from second table.The Result set should like,Row From Table1 : 1 JackRow From Table2 : 1 LondonRow From Table1 : 2 PhilipRow From Table2 : 2 Glascowlike the above....Regards,Jagadhees. |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-03-15 : 12:51:30
|
I understand what a JOIN expression is, but you have not indicated why you feel one is necessary. To acheive the results you have indicated all you need is:Select Id,NameFrom Table1unionselect id, AddressFrom Table2Unless there is more sample data or more information you can give us, it is impossible for us to give you what you are looking for. Remember, we don't work at your company and we don't know what problems you are facing or what data you have or what requirements you are trying to meet. Does this make sense?- Jeff |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-03-15 : 13:18:27
|
Join or union, it's up to you.Got a copy of books online?USE NorthwindGOSET NOCOUNT ONCREATE TABLE myTable99(Col1 int, Col2 varchar(15))CREATE TABLE myTable00(Col1 int, Col2 varchar(15))GOINSERT INTO myTable99(Col1, Col2)SELECT 1, 'Jack' UNION ALLSELECT 2, 'Philip'INSERT INTO myTable00(Col1, Col2)SELECT 1, 'London' UNION ALLSELECT 2, 'Glascow'GOSELECT Col1, Col2 FROM ( SELECT Col1, Col2, 1 AS TableOrder FROM myTable99 UNION ALL SELECT Col1, Col2, 2 AS TableOrder FROM myTable00 ) AS XXXORDER BY Col1, TableOrder-- OR SELECT l.Col1, r.Col2+', '+l.Col2 AS FullName FROM myTable99 l INNER JOIN myTable00 r ON l.Col1 = r.Col1GOSET NOCOUNT OFFDROP TABLE myTable99DROP TABLE myTable00GO Brett8-) |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-03-15 : 14:36:40
|
quote: Originally posted by X002548 SELECT l.Col1, r.Col2+', '+l.Col2 AS FullName FROM myTable99 l INNER JOIN myTable00 r ON l.Col1 = r.Col1
Yeah but that doesn't give the expected result set. He wants four rows. You can't do that with a join.Tara |
 |
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-03-15 : 18:24:13
|
Tara is right so far, but with a little cheating...Jagadhees - WHY???CREATE TABLE table1(Id int, Name varchar(15))CREATE TABLE table2(Id int, Address varchar(15))INSERT INTO table1(Id, Name)SELECT 1, 'Jack' UNION ALLSELECT 2, 'Philip' UNION ALLSELECT 3, 'Peter'INSERT INTO table2(Id, Address)SELECT 1, 'London' UNION ALLSELECT 2, 'Glascow' UNION ALLSELECT 3, 'Pan'SELECT table1.Id, CASE d WHEN 1 THEN Name ELSE Address ENDFROM table1 CROSS JOIN (SELECT 1 AS d UNION SELECT 2) dJOIN table2 ON table1.ID = table2.IDDROP TABLE table1DROP TABLE table2 rockmoose |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-03-16 : 09:14:31
|
quote: Originally posted by tduggan
quote: Originally posted by X002548 SELECT l.Col1, r.Col2+', '+l.Col2 AS FullName FROM myTable99 l INNER JOIN myTable00 r ON l.Col1 = r.Col1
Yeah but that doesn't give the expected result set. He wants four rows. You can't do that with a join.Tara
My first Union doesn't give hime what he asked for?Brett8-) |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-03-16 : 12:31:05
|
The UNION does, but the JOIN does not. I wonder how his class is going.Tara |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-04-12 : 14:22:24
|
Yes...what grade did you get?Brett8-) |
 |
|
|