Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-14-2012, 03:04 AM   PM User | #1
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 302
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
how to join tables with same field name.

How do I join two tables that have the same field name and group them so only one of each is returned as results from both tables may have the same `userid`
needsomehelp is offline   Reply With Quote
Old 12-14-2012, 04:07 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I can't tell from your description whether you really need a JOIN or if you need a UNION.

I think you need to show us a specific example of what you are after.

Regardless of whether you need a JOIN or UNION, how will you tell MySQL which one of the pair to pick?

In other words if I have
Code:
-- Table1 --
userid   frogwarts
   117    greenish pink
   118    black
   119    blue

-- Table2 --
userid   frogwarts
   116    yellowish mauve
   117    puce
   118    black
What do you want for the final result? For userids 116, 118, and 119 it is easy. Only one table has userids 116 and 119. And both have the same frogwarts value for userid 188.

But what do we show for 117? greenish pink or puce??

How do we tell the computer which one to pick?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-14-2012, 08:39 AM   PM User | #3
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 302
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
sorry, i mean this...

-- Table1 --
userid
117
118
119

-- Table2 --
userid
116
117
118

and return only one of each userid.

117
118
119
116
needsomehelp is offline   Reply With Quote
Old 12-14-2012, 08:57 AM   PM User | #4
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 302
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
think i found it, seems to show the right results.

SELECT `userid` FROM `table1`
UNION
SELECT `userid` FROM `table2`
ORDER BY `userid`
needsomehelp is offline   Reply With Quote
Old 12-14-2012, 07:54 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Yes, that works for what you asked for.

But watch out.

Suppose you had the tables that I showed:
Code:
-- Table1 --
userid   frogwarts
   117    greenish pink
   118    black
   119    blue

-- Table2 --
userid   frogwarts
   116    yellowish mauve
   117    puce
   118    black
If you then did
Code:
SELECT userid, frogwarts FROM table1
UNION
SELECT userid, frogwarts FROM table2
You would end up with
Code:
userid   frogwarts
   116    yellowish mauve
   117    puce
   117    greenish pink
   118    black
   119    blue
UNION only gives you a single result from multiple tables if *ALL* the fields from both (or all) tables are the same. If any one field is different, then you get full results from both (or all) tables.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-14-2012, 08:11 PM   PM User | #6
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 302
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
would this be correct?
Code:
SELECT userid FROM table1
UNION
SELECT userid FROM table2
GROUP BY userid
ORDER BY userid
needsomehelp is offline   Reply With Quote
Old 12-15-2012, 02:43 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Yes. You don't need the GROUP BY, though. It's not hurting anyting; it's just not doing anything in this particular query.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:18 AM.


Advertisement
Log in to turn off these ads.