MySqlTalk.com  

Go Back   MySqlTalk.com > MySQL > Database Design
User Name
Password
FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
Thread Tools Search this Thread
Old 02-06-2005, 04:45 PM   #1
Azkaban
Senior Member
 
Join Date: Jul 2004
Posts: 167 Azkaban is on a distinguished road
What is the "right" join for the job?

I want to match a field to 10 tables. It appears in some tables once, in other tables twice, in other tables not at all. I can't seem to find the right join for the job. I either get too many rows, not enough rows or incorrect rows.
Can someone help please?

Table 1
=============
t1id | userid | flag | score
--------------------------
1 | 1003 | red | 10

2 | 1003 | red | 5

3 | 1005 | blue | 15

Table 2
=============
t2id | userid | flag | score
--------------------------
1 | 1012 | yellow | 7

2 | 1002 | green | 4

3 | 1001 | red | 1

Table 3
=============
t1id | userid | flag | score
--------------------------
1 | 1012 | orange | 10

2 | 1022 | blue | 10

3 | 1023 | blue | 10
================================================== ========
I want to join these three tables on the value of red and get no data for table 3 and two rows for table 1. So something like this should be the result:
----------------------
Result
=============
userid | flag | score
--------------------------
1003 | red | 10
1003 | red | 5
1001 | red | 1
================================================== ========
Azkaban is offline   Fork this post Reply With Quote
Old 02-25-2005, 02:36 PM   #2
gertcuppens
Member
 
Join Date: Feb 2005
Location: ANtwerp - Belgium
Posts: 34 gertcuppens is on a distinguished road
Re: What is the "right" join for the job?

This seems to me like you need a union.

You should try this statement :

select * from T1
where flag = "red"
UNION
select * from T2
where flag = "red"
UNION
select * from T3
where flag = "red"
gertcuppens is offline   Fork this post Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cross dependency found in OUTER JOIN SQL rjmthezonenet SQL syntax 0 12-05-2004 04:41 PM
help with join shaunperry SQL syntax 0 10-07-2004 06:22 AM



All times are GMT -4. The time now is 06:09 AM.



Powered by: vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
Google
  Web http://www.mysqltalk.com
DISCLAIMERS:
1. We have no commercial interest in this site.
Banner Ads and Subscriptions will only be used to help pay for hosting and maintenance costs.
2. MySQLTalk.com is NOT affiliated with MySQL AB in any way.
3. MySQLTalk.com is NOT endorsed by MySQL AB in any way.
4. Please do not post any content that is harmful to MySQL or MySQL AB, meaning no misleading or obsolete information will be tolerated.
Well-founded constructive criticism meant to help the community is permitted.
5. This website is founded with the goal of improving the MySQL community.
We not only tolerate newbies, we encourage them.
Please do not ask newbies to "read the manual".