[英]How to do the following MySQL join?
Assume I have the following tables: 假设我有以下表格:
tableA TABLEA
a_name | age | country
Jordan | 5 | Germany
Molly | 6 | Spain
Paris | 7 | France
John | 7 | Saudi Arabia
John | 5 | Saudi Arabia
John | 6 | Spain
tableB tableB的
id (auto increment primary key) | age | country
1 | 5 | Germany
2 | 6 | Spain
3 | 7 | France
4 | 7 | Spain
5 | 8 | France
6 | 9 | France
7 | 2 | Mexico
8 | 7 | Saudi Arabia
9 | 5 | Saudi Arabia
I want to be able to do some kind of select where I am able to get: 我希望能够在某些地方进行选择:
tableA TABLEA
a_name | age | country | id
Jordan | 5 | Germany | 1
Molly | 6 | Spain | 2
Paris | 7 | France | 3
John | 7 | Saudi Arabia | 8
John | 5 | Saudi Arabia | 5
John | 6 | Spain | 4
Iteratively I fill in what is in the "id" field by looking up what is in tableB for the age and country pair. 迭代地,我通过查找tableB中年龄和国家/地区对的内容来填写“id”字段中的内容。 Is there some SQL query/queries I can do to add that new "id" column which is based on that of table B without having to use a cursor?
是否有一些SQL查询/查询我可以添加基于表B的新“id”列而不必使用游标?
The previous answer is correct, but ignores a couple of problems in the way you are approaching the issue. 之前的答案是正确的,但忽略了您处理问题的方式中的几个问题。
You should have your Country
table referenced by a country_id
field in your kids
table: that way you are not duplicating the info (what happens in your design when a country changes its name?). 你应该有你的
Country
被引用的表country_id
场在你kids
表:你是不是复制信息这种方式(什么你的设计发生在一国改变其名字吗?)。 You are also making a poor choice by storing the age of the people: you should store the date (or year) of birth instead, to avoid having to recalculate their age. 你也可以通过存储人的年龄做出糟糕的选择:你应该存储出生日期(或年份),以避免重新计算他们的年龄。
Kids table: 儿童餐桌:
name | DOB | country_id | id
Joe | Aug 11 2005 | 1 | 1
Jim | Sep 09 2007 | 21 | 2
Countries table: 国家表:
id | name
1 | UK
2 | Germany
Then the select statement you need is: 然后你需要的select语句是:
select kids.name, kids.DOB, countries.name, kids.id from kids join countries on kids.country_id = countries.id
You can then either calculate the ages in your application, or you can incorporate a calculation into your SQL, see (for example) http://ma.tt/2003/12/calculate-age-in-mysql/ 然后,您可以在应用程序中计算年龄,或者可以将计算结合到SQL中,请参阅(例如) http://ma.tt/2003/12/calculate-age-in-mysql/
Edit 编辑
After reading your clarification that the age column is the age of the person at the time of their visit to the country , I would suggest a three-table design instead, still following the same principles of storing dates, not ages, and only storing each item of data once: 在阅读你的澄清说明年龄栏是他们访问该国时的人的年龄时 ,我建议改为采用三桌设计,仍然遵循相同的存储日期而不是年龄的原则,并且只存储每个数据项一次:
People table 人表
Structure 结构体
CREATE TABLE `countries` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(22) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
Sample content 样本内容
id | name | DOB
1 | Joe | 2005-08-11
2 | Jim | 2007-09-09
3 | Carol | 2008-01-22
Countries table 国家表
Structure 结构体
CREATE TABLE `countries` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(22) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
Sample data 样本数据
id | name
1 | UK
2 | Germany
3 | Saudi Arabia
Visits table 访问表
Structure 结构体
CREATE TABLE `visits` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`person_id` int(11) unsigned NOT NULL,
`country_id` int(11) unsigned NOT NULL,
`date_of_visit` date NOT NULL,
PRIMARY KEY (`id`),
KEY `person_id` (`person_id`),
KEY `country_id` (`country_id`),
CONSTRAINT `visits_ibfk_1` FOREIGN KEY (`person_id`) REFERENCES `people` (`id`),
CONSTRAINT `visits_ibfk_2` FOREIGN KEY (`country_id`) REFERENCES `countries` (`id`)
) ENGINE=InnoDB;
Sample data 样本数据
id | person_id | country_id | date_of_visit
1 | 1 | 1 | 2009-01-02
2 | 1 | 2 | 2010-01-01
Then the query you need to get the data you want is: 然后,您需要获取所需数据的查询是:
select
people.name,
people.DOB,
countries.name,
visits.date_of_visit
from
visits
join
countries
on
visits.country_id = countries.id
join
people
on
visits.person_id = people.id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.