MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 10th, 2012, 06:53 PM
dikoB dikoB is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 82 dikoB User rank is Corporal (100 - 500 Reputation Level)dikoB User rank is Corporal (100 - 500 Reputation Level)dikoB User rank is Corporal (100 - 500 Reputation Level)dikoB User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 3 h 1 m 33 sec
Reputation Power: 9
Join 2 Select From 2 Tables

I wanted to join 2 select stmt from 2 tables.

TABLE A (3 rows)
ID | NAME
111 | AAA
111 | BBB
111 | CCC
222 | III
222 | JJJ
333 | KKK

TABLE B (4 rows)
ID | LAST
111 | EEE
111 | FFF
111 | GGG
111 | HHH
222 | PPP
222 | QQQ
222 | TTT
333 | VVV
333 | UUU

I wanted to have a result like this:
RESULT:

ID | NAME | LAST
111 AAA EEE
111 BBB FFF
111 CCC GGG
111 NULL* HHH

I tried the following query but got duplicate results.
select A.name, B.last from A, B where a.id = 111 and b.id = 111

Any ideas how to get the sample result above w/o duplicates?

Thanks,

Last edited by dikoB : December 11th, 2012 at 01:51 PM.

Reply With Quote
  #2  
Old December 10th, 2012, 07:11 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,371 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 28 m 44 sec
Reputation Power: 4140
based on the data you posted, it's impossible, because there are no columns that can be used to identify which rows go with which rows

try copy/paste using excel
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old December 10th, 2012, 07:26 PM
dikoB dikoB is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 82 dikoB User rank is Corporal (100 - 500 Reputation Level)dikoB User rank is Corporal (100 - 500 Reputation Level)dikoB User rank is Corporal (100 - 500 Reputation Level)dikoB User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 3 h 1 m 33 sec
Reputation Power: 9
Quote:
Originally Posted by r937
based on the data you posted, it's impossible, because there are no columns that can be used to identify which rows go with which rows

try copy/paste using excel

Thanks. The tables are just representation though but I'm not sure if it's doable. Basically I just wanted to have an enumerated results from 2 tables side by side having same ID of 111. Notice the last row is NULL under "NAME" since TABLE A has only 3 rows.

Reply With Quote
  #4  
Old December 10th, 2012, 09:02 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,371 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 28 m 44 sec
Reputation Power: 4140
but ~all~ the rows have an id of 111

how's it supposed to know that AAA goes with EEE

Reply With Quote
  #5  
Old December 11th, 2012, 11:38 AM
dikoB dikoB is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 82 dikoB User rank is Corporal (100 - 500 Reputation Level)dikoB User rank is Corporal (100 - 500 Reputation Level)dikoB User rank is Corporal (100 - 500 Reputation Level)dikoB User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 3 h 1 m 33 sec
Reputation Power: 9
Quote:
Originally Posted by r937
but ~all~ the rows have an id of 111

how's it supposed to know that AAA goes with EEE

Okay thanks, it seems like there's no other way for this but to have a separate query on both tables

Reply With Quote
  #6  
Old December 12th, 2012, 04:00 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
One query or two queries. It makes no difference. There's still no way of relating 'aaa' and 'eee'.

Reply With Quote
  #7  
Old December 14th, 2012, 02:48 AM
jaysh4922 jaysh4922 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 31 jaysh4922 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 5 m 22 sec
Reputation Power: 1
SELECT *
FROM tbl1 t1
LEFT JOIN tbl2 t2
ON t1.col = t2.col
UNION
SELECT *
FROM tbl1 t1
RIGHT JOIN tbl2 t2
ON t1.col>= t2.<c

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Join 2 Select From 2 Tables

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap