mySql在非唯一列上加入两个表

时间:2011-06-09 14:47:09

标签: mysql join

您有以下表格:

表区:

id   DD   text   
---|----|-----
1  | 01 | texas     
2  | 02 | ny     
5  | 03 | washington   
表理事会:

id   DD   CC   text
---|----|----|-----
1  | 01 | 01 | text1 
2  | 02 | 01 | text2
5  | 02 | 02 | text3
3  | 02 | 03 | text4
4  | 03 | 01 | text5
5  | 03 | 02 | text6
6  | 01 | 02 | text7
表人:

id   name    DD   CC
---|-------|----|----
1  | john  | 02 | 03 
2  | mike  | 03 | 02
3  | julia | 01 | 02

我想进行查询,以便得到以下结果:

结果:

 name     District    Council
-------|------------|-------
 john  | ny         | text4 
 mike  | washington | text6
 julia | texas      | text7

到目前为止,我有以下查询:

select p.name,d.text as district,c.text as council
    from  person p 
        inner join districts d on p.DD=d.DD
        inner join councils c on p.DD=c.DD and p.CC=c.CC
where 1;

我认为逻辑应该是正确的,但不知怎的,我得到一个错误......任何人都可以帮助理解这一点或指出我正确的方向吗?

如果我这样做,它会返回结果,但当然不是预期的结果:

select p.name,d.text as district,c.text as council
    from  person p 
        inner join districts d on p.DD=d.DD
        inner join councils c on p.CC=c.id
where 1;

由于

编辑:这是一个整理问题...我的建议如果你发现这是不使用查询浏览器'导致错误不是很冗长...可能有一个选项,但我不知道!< / p>

谢谢大家

2 个答案:

答案 0 :(得分:3)

我不知道你的架构,但这就是我所做的(它工作得很好!)

CREATE TABLE `districts` (
    `id` INT(10) unsigned NOT NULL,
    `DD` CHAR(20) NOT NULL,
    `text` CHAR(100) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `councils` (
    `id` INT(10) unsigned NOT NULL,
    `DD` CHAR(20) NOT NULL,
    `CC` CHAR(20) NOT NULL,
    `text` CHAR(100) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `person` (
    `id` INT(10) unsigned NOT NULL,
    `DD` CHAR(20) NOT NULL,
    `CC` CHAR(20) NOT NULL,
    `name` CHAR(100) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO districts (`id`, `DD`, `text`) VALUES ('1','01','texas');   
INSERT INTO districts (`id`, `DD`, `text`) VALUES ('2','02','ny');   
INSERT INTO districts (`id`, `DD`, `text`) VALUES ('5','03','washington');

INSERT INTO councils (`id`, `DD`, `CC`, `text`) VALUES ('1','01','01','text1');
INSERT INTO councils (`id`, `DD`, `CC`, `text`) VALUES ('2','02','01','text2');
INSERT INTO councils (`id`, `DD`, `CC`, `text`) VALUES ('3','02','03','text4');
INSERT INTO councils (`id`, `DD`, `CC`, `text`) VALUES ('4','03','01','text5');
INSERT INTO councils (`id`, `DD`, `CC`, `text`) VALUES ('5','03','02','text6');
INSERT INTO councils (`id`, `DD`, `CC`, `text`) VALUES ('6','01','02','text7');

INSERT INTO person (`id`, `name`, `DD`, `CC`) VALUES ('1','john','02','03');
INSERT INTO person (`id`, `name`, `DD`, `CC`) VALUES ('2','mike','03','02');
INSERT INTO person (`id`, `name`, `DD`, `CC`) VALUES ('3','julia','01','02');

SELECT p.name, d.text AS district, c.text AS council
FROM person p
INNER JOIN districts d ON p.DD = d.DD
INNER JOIN councils c ON p.DD = c.DD
AND p.CC = c.CC
WHERE 1

答案 1 :(得分:0)

你的第一个查询应该有效......只删除WHERE 1 ...如果你要从“person”表中返回所有记录,你根本不需要where子句。您的表别名使用是正确的,并且第一个查询正确地连接到区和理事会的理事会,否则您的第二个示例查询将为一个人返回多行,因为只有重复的“理事会”代码。

“WHERE 1”失败...因为1不是逻辑表达式...如果使用,你将不得不做“WHERE 1 = 1”

相关问题