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 > extracting data from a multifield column

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-09-09, 16:02
cwebbe2 cwebbe2 is offline
Registered User
 
Join Date: Feb 2009
Posts: 15
extracting data from a multifield column

Hi,

This is my first post and I think I know all the rules... and possibly have a good question.

I am trying to extract some data from a SQL database at work and have run into a multifield column that is like what follows:

pkid action userID multifield_1 Multifield_2

12 1 22 233 4
13 1 22 234 4
14 2 23 12 Blue


In this case, if action is set at 1 I get information on pkid 12 about multifield 1 being 233 and multifield 2 being 4, etc I understand this.

If I select for action 2, I get a separate group of data to include multifield2 showing blue, etc. Again, no problems.


Where my tiny head cannot comprehend and gets confused is if I wanted to get data on pkid 14, action 2, but instead of getting "12" or "blue" instead have the "12 link back to the pkid two lines up and return results "233" and "4."

Is this possible?


Thank you for any help.

Chris
Reply With Quote
  #2 (permalink)  
Old 02-09-09, 16:20
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Code:
SELECT a.pkid
     , a.action
     , a.userid
     , b.multifield_1
     , b.multifield_2
FROM   your_table As [a]
 LEFT
  JOIN your_table As [b]
    ON a.multifield_1 = b.pkid
WHERE  a.pkid = 14
AND    a.action = 2
...I think I got the right end of the stick...
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 02-10-09, 08:23
cwebbe2 cwebbe2 is offline
Registered User
 
Join Date: Feb 2009
Posts: 15
This worked perfectly. And makes so much sense (as a lot of SQL does, when you think about it... but apparently not all of it).

Thank you very much!
Reply With Quote
  #4 (permalink)  
Old 02-10-09, 09:33
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
george, you simply must slow down and take the time to learn mysql syntax

otherwise you be always holding da brown sticky end of da stick

those square brackets are valid only in microsoft (~ptui~) databases

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 02-10-09, 09:59
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Sorry Mr Limeback

Old habits and all that! What do the standards suggest as identifiers? I know mysql has backticks (~ptui~) for objects... what's your preference?
__________________
George
Twitter | Blog
Reply With Quote
  #6 (permalink)  
Old 02-10-09, 10:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by georgev
what's your preference?
using identifiers (column and table names and aliases) that do not require escaping

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 02-10-09, 10:42
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Well I know they are superfluous in my usage because I do not use names that require escaping, but it's a habit I got in to because I found that it makes it easier to "spot" aliases. I previosuly used single-quotes, but found that this could be misleading, hence the switch.
__________________
George
Twitter | Blog
Reply With Quote
  #8 (permalink)  
Old 02-10-09, 10:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
easier to "spot" aliases?

there is a much better way... let me show u it!!
Code:
SELECT a.pkid
     , a.action
     , a.userid
     , b.multifield_1
     , b.multifield_2
  FROM your_table                  AS a
LEFT OUTER
  JOIN your_table                  AS b
    ON b.pkid = a.multifield_1
 WHERE a.pkid = 14
   AND a.action = 2
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 02-10-09, 10:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
by the way, you could also use the standard ANSI doublequote escape characters, but you need to make sure that the ANSI_QUOTES SQL mode is enabled

does microsoft Squeal Server give you that option?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 02-10-09, 11:19
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Yeah, I did a similar thing to that before too, but found that it got rather verbose in simple text-editors (word-wrap for long lines etc)...

yes SQL Server does allow double quotes, I think it might even be by default, but even if it's not then it's definately a setting (SET QUOTED_IDENTIFIER if memory serves).
__________________
George
Twitter | Blog
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