If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Query Help~!!!!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-16-10, 04:58
kalons kalons is offline
Registered User
 
Join Date: Dec 2010
Posts: 1
Red face Query Help~!!!!

Hi guys, thanks in advance to see my topic.
Here is my problem:

There is two tables named Factory Table & Office Table,and see below:

Factory Table
FID --- OID
1 ------ 1,2
2 ------ 3
3 ------ 2,4,5
4 ---- --- 5


Office Table
OID --- OfficeName
1 ----- - -- A
2 ------ --- B
3 ------ --- C
4 --- -- -- D
5 --- ------ E


What I want to output
FID ------- Name
1 ------ -- A,B
2 ------ -- C
3 --- --- -- B,D,E
4 ------ -- E


Please help~! Thank you so much!
Reply With Quote
  #2 (permalink)  
Old 12-16-10, 06:49
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
Can you confirm that in Factory table you have OID as a comma separated value of the identifiers of the offices ID?

If that is the case then writing a single query might be difficult. If on the other hand you have multiple entries:

Factory Table
FID --- OID
1 ------ 1
1 ------ 2
2 ------ 3
3 ------ 2
3 ------ 4
3 ------ 5
4 ------ 5

Having this type of setup will simplify your SQL statement. Have a look at GROUP_CONCAT function to give you the results in the comma separated way for your query.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 12-16-10, 08:44
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by it-iss.com View Post
...writing a single query might be difficult.
oh, it's not all that difficult, it's just slow as cold glue because it requires a table scan
Code:
SELECT f.fid
     , GROUP_CONCAT(o.officename) AS offices
  FROM factory AS f
INNER
  JOIN office AS o
    ON FIND_IN_SET(o.oid,f.oid)
GROUP
    BY f.fid
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 12-16-10, 09:49
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
Nice function FIND_IN_SET. Didn't know it existed and you are right about the overall performance too.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On