3 messages in com.mysql.lists.mysqlSELECTing to Substitute Text for Refc...
FromSent OnAttachments
Greg Willits24 May 2004 17:12 
Greg Willits25 May 2004 00:25 
Roddie Grant25 May 2004 01:52 
Subject:SELECTing to Substitute Text for Refc Codes
From:Greg Willits (gr@fivegeeks.com)
Date:05/24/2004 05:12:17 PM
List:com.mysql.lists.mysql

Struggling with a query structure new to me...

Most of my apps are not very complex at all (simple joins, few functions), but I've worked through a few "tricky" queries. However, this one is stumping me on how to even begin. I can't even decide what kind of query it is. As you can I wasn't even sure what to call it :-(

I'll distill it to a structural example. Two tables: first table (contacts) has basic contact stuff (Name, city, State), second table (syscodes) is a arbitrary collection of "codes" and "descriptions" (code, desc).

The contacts table uses codes from the sysycodes table instead of actual text for stuff like city and state. So, the contacts table might look like:

Will Doolittle, 1543, 425 Robin Banks, 1800, 12897

And, the syscodes table is

425, California 1543, Anaheim 1800, Topeka 12897, Kansas

In my realm this is a sadistic level of normalization, but I suppose it is pretty common and must be useful at some scale (I'm getting data from a large U.S. county). Either I'm thinking too hard, or really don't get it, but I don't know how to pull those together to get:

Will Doolittle, Anaheim, California Robin Banks, Topeka, Kansas

So starting with the basics:

SELECT cntcName, cntcCity, cntcState FROM contacts, syscodes WHERE cntcID="x" AND cntcCity=code AND cntcState=code

(which I know is wrong)

But how to connect which row from syscodes goes to which cntc field? The only way I can seem to visualize this is as a bunch of substitutions to alias with AS in the select phrase, but I don't see how.

Unfortunately, this has to be compatible with 3.23.54. (when 4.1 hits beta then I will likely switch to it for this particular app as I need sub-selects for another task).

All clues welcomed.