MYSQL newbie needs some help with a crazy query! (Joins, foreign keys, and bears "oh my!")
Hallo, all. So I'm just starting to get the hang on writing mysql queries with JOINS, but I'm having the most difficult time with what I believe is a rather simple query. I'm wondering if someone here can help me out. I've got a rather complicated database of customers with a whole bunch of related tables and foreign keys. (MYSQL structure is pasted below)
Basically, I need to write a query that will a output all customers with the appropriate labels replacing the ugly "foreign keys" that are in there. I've been screwing around with joins for the better part of two days but I just can't wrap my mind around what should be (I think) a very simple query. Something akin to:
first_name | last_name | email | gender | customer_types.description | city | states.abbrev | zip | countries.name | ethnic_backgrounds.name | interest.interest_title(1) | interest.interest_title(2) | interest.interest_title(2) | inquiry_sources.sourcename
Can any query wizards out there lend this newbie a hand?
CREATE TABLE `customers` (
`id` int(10) unsigned NOT NULL auto_increment,
`first_name` varchar(255) NOT NULL default '',
`last_name` varchar(255) NOT NULL default '',
`email` varchar(255) NOT NULL default '',
`gender` varchar(10) NOT NULL default '',
`customer_type_id` int(10) unsigned NOT NULL default '0',
`city` varchar(255) NOT NULL default '',
`state_id` int(10) unsigned NOT NULL default '0',
`zip` varchar(255) NOT NULL default '',
`country_id` int(10) NOT NULL default '0',
`ethnic_background_id` int(10) unsigned NOT NULL default '0',
`interest1_id` int(10) unsigned NOT NULL default '0',
`interest2_id` int(10) unsigned NOT NULL default '0',
`interest3_id` int(10) unsigned NOT NULL default '0',
`inquiry_source_id` int(10) unsigned NOT NULL default '0'
PRIMARY KEY (`id`)
)
CREATE TABLE `customer_types` (
`id` int(10) unsigned NOT NULL auto_increment,
`description` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
)
CREATE TABLE `states` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
`abbrev` varchar(4) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=67 ;
CREATE TABLE `countries` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) NOT NULL default '',
PRIMARY KEY (`id`)
)
CREATE TABLE `ethnic_backgrounds` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
)
TABLE `interests` (
`Record_id` int(11) NOT NULL auto_increment,
`interest_title` varchar(255) NOT NULL default '',
PRIMARY KEY (`Record_id`)
)
CREATE TABLE `inquiry_sources` (
`id` int(10) unsigned NOT NULL auto_increment,
`sourcename` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
)
Thanks in advance for any help.
FROM customers
LEFT JOIN states ON (customers.state_id=states.id)
LEFT JOIN inquiry_sources ON (customers.inquiry_source_id=inquiry_source.id)
...
;
posted by cmiller at 10:42 AM on February 25, 2008