many to many problem
am 19.11.2006 21:51:29 von stevekHaving problem with many to many join. Below is the schema with tables
and sql statements. All seems to work with the following exception: Can
not get all the field names to display.
// problem is can not get 'member name' to display
// if put 'member.member_lname' in select statement get error msg
// Unknown column 'member.member_lname' in 'field list'
select member_cccp_lnk.member_dist_no, member_cccp_lnk.cccb_id,
cccb.com_name
from member_cccp_lnk
left join cccb on (cccb.cccb_id = member_cccp_lnk.cccb_id)
where member_cccp_lnk.member_dist_no = '0008';
// problem is can not get 'cccb name' to display
// if put 'cccb.com_name' in select statement get error msg
// Unknown column 'cccb.com_name' in 'field list'
select member.member_fname, member.member_lname
from member_cccp_lnk
left join member on (member.member_dist_no = member_cccp_lnk.member_dist_no)
where member_cccp_lnk.cccb_id = '0200';
create database cccb;
use cccb;
DROP TABLE IF EXISTS `cccb`.`cccb`;
CREATE TABLE `cccb`.`cccb` (
`cccb_id` int(4) unsigned zerofill NOT NULL,
`com_name` varchar(50) default NULL,
PRIMARY KEY (`cccb_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
insert into cccb (cccb_id, com_name) values
(150 ,'Annexation C'),
(200 ,'Advisory C'),
(800 ,'Protection C'),
(1000 ,'Transit C'),
(1050 ,'Water C');
DROP TABLE IF EXISTS `cccb`.`member`;
CREATE TABLE `cccb`.`member` (
`member_dist_no` int(4) unsigned zerofill NOT NULL,
`member_fname` varchar(14) NOT NULL,
`member_lname` varchar(14) NOT NULL,
PRIMARY KEY (`member_dist_no`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
insert into member (member_dist_no, member_fname, member_lname) values
(01, "Jerry", "Jones"),
(02, "Thomas", "Smith"),
(03, "Jack", "Nicholson"),
(04, "Anthony", "Ant"),
(05, "Gary", "Greece"),
(06, "Chad", "Chad"),
(07, "Celestine", "Pasta"),
(08, "Christopher", "Chris"),
(09, "Guss", "Guy"),
(10, "Steve", "Sterling"),
(11, "John", "Jones"),
(12, "Thomas", "Tomas");
DROP TABLE IF EXISTS `cccb`.`member_cccp_lnk`;
CREATE TABLE `cccb`.`member_cccp_lnk` (
`member_dist_no` int(4) unsigned zerofill NOT NULL,
`cccb_id` int(5) unsigned zerofill NOT NULL,
PRIMARY KEY (`member_dist_no`,`cccb_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
insert into member_cccp_lnk (member_dist_no, cccb_id) values
(01, 150),
(01, 200),
(01, 550),
(01, 600),
(01, 650),
(01, 850),
(02, 450),
(02, 650),
(02, 800),
(03, 400),
(03, 800),
(04, 350),
(04, 1050),
(05, 550),
(05, 950),
(06, 200),
(06, 550),
(06, 650),
(06, 800),
(06, 850),
(07, 100),
(07, 200),
(07, 250),
(07, 550),
(07, 650),
(08, 200),
(08, 800),
(08, 1000),
(09, 500),
(09, 550),
(10, 350),
(11, 200),
(11, 300),
(11, 350),
(12, 200),
(12, 350),
(12, 900);
select member_cccp_lnk.member_dist_no, member_cccp_lnk.cccb_id,
cccb.com_name
from member_cccp_lnk
left join cccb on (cccb.cccb_id = member_cccp_lnk.cccb_id)
where member_cccp_lnk.member_dist_no = '0008';
select member.member_fname, member.member_lname
from member_cccp_lnk
left join member on (member.member_dist_no = member_cccp_lnk.member_dist_no)
where member_cccp_lnk.cccb_id = '0200';