MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.

Learn More!


Download to Enter
| Contest Rules

Tutorials | Forums

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 February 4th, 2012, 10:44 AM
Pavcio Pavcio is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 2 Pavcio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 34 sec
Reputation Power: 0
[MySQL] Relations, connect by two fields

Hello,

I request to you for help in solving my problem. I have two tables in a database that I need to connect in a fairly specific way.

Code:
TABLE 1: contains a list of all registered users

id     username     
1      user1          
2      user2         
3      user3        
4      user4        
5      user5        


Code:
TABLE 2: lists the files that have been flagged by users as being inconsistent with the terms and conditions

id     user_id              uploader_id
1      1                       3 
2      2                       3
3      3                       2
4      1                       3
5      1                       4      


How to extract of Table 1 only those users who appeared in table2 in field: user_id or uploader_id? I need to create links to users details based on their username (no id) so I need to connect in some way, these tables. I'll be very grateful for your help.

Regards

Reply With Quote
  #2  
Old February 4th, 2012, 06:09 PM
fubes2000's Avatar
fubes2000 fubes2000 is offline
manwich
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2003
Location: Canadanistan
Posts: 575 fubes2000 User rank is Major (30000 - 40000 Reputation Level)fubes2000 User rank is Major (30000 - 40000 Reputation Level)fubes2000 User rank is Major (30000 - 40000 Reputation Level)fubes2000 User rank is Major (30000 - 40000 Reputation Level)fubes2000 User rank is Major (30000 - 40000 Reputation Level)fubes2000 User rank is Major (30000 - 40000 Reputation Level)fubes2000 User rank is Major (30000 - 40000 Reputation Level)fubes2000 User rank is Major (30000 - 40000 Reputation Level)fubes2000 User rank is Major (30000 - 40000 Reputation Level)fubes2000 User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 388781 Folding Title: Super Ultimate Folder - Level 1Folding Points: 388781 Folding Title: Super Ultimate Folder - Level 1Folding Points: 388781 Folding Title: Super Ultimate Folder - Level 1Folding Points: 388781 Folding Title: Super Ultimate Folder - Level 1Folding Points: 388781 Folding Title: Super Ultimate Folder - Level 1Folding Points: 388781 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Week 10 h 47 m
Reputation Power: 382
Something like:
Code:
SELECT ...
FROM table1 t1 INNER JOIN table2 t2
	ON ( t1.id = t2.user_id
		OR t1.id = t2.uploader_id )
WHERE t1.username LIKE 'ted_danson';
__________________

Reply With Quote
  #3  
Old February 4th, 2012, 08:04 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,046 r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Days 22 h 44 sec
Reputation Power: 3829
Quote:
Originally Posted by fubes2000
Code:
	ON ( t1.id = t2.user_id
		OR t1.id = t2.uploader_id )
this can be simplified to...
Code:
ON t1.id IN ( t2.user_id , t2.uploader_id )
note that my parentheses are required but yours are superfluous

Comments on this post
fubes2000 agrees: I thought this might work, but I've never used an IN in this way before. Parentheses are a force of
habit I guess.
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #4  
Old February 4th, 2012, 08:51 PM
Pavcio Pavcio is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 2 Pavcio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 34 sec
Reputation Power: 0
Both methods work very well. Thank you.

Now another problem.
I need a list of users who are in the field user_id, uploader_id in Table 2.
The problem is that the table1 has more than 1 million records so to create a list I need to use pagaination. To do pagination I need to know how many users appears in the fields user_id, uploader_id in table 2.

I used this query:

Code:
SELECT COUNT(DISTINCT(t1.id))
FROM table1 t1 INNER JOIN table2 t2
ON t1.id IN ( t2.user_id , t2.uploader_id )


It's takes far too long. Could you prompt some other solution?

Reply With Quote
  #5  
Old February 4th, 2012, 09:09 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,046 r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Days 22 h 44 sec
Reputation Power: 3829
Quote:
Originally Posted by Pavcio
Could you prompt some other solution?
use the SQL_CALC_FOUND_ROWS option on the SELECT statement

the number of rows can then be retrieved with SELECT FOUND_ROWS()

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > [MySQL] Relations, connect by two fields


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 - 2012, Jelsoft Enterprises Ltd.

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