[英]MySQL - find difference between fields in same column
I have a table containing first name, middle name, last name, year of birth, municipality and income details. 我有一个表格,其中包含名字,中间名,姓氏,出生年份,城市和收入详细信息。 The data set contains several years.
数据集包含数年。 The problem is that the data does not include a unique id per person.
问题在于数据不包含每人唯一的ID。
Thus, I have to compare the same people based on the abovementioned data to find the same people for each year. 因此,我必须根据上述数据比较同一个人,以找到每年的同一个人。 Not exactly precise but there is no other way.
不够精确,但是没有其他方法。
I'm trying to write a query that compare the income in one year with another to see how the income develops (drops, rises). 我正在尝试编写一个查询,将一年与另一年的收入进行比较,以查看收入如何增长(下降,上升)。
My try so far: 到目前为止,我的尝试:
SELECT e.firstname, e.lastname, e.birth, e.middlename,
e.income-n.income as incomediff
from incomedata e
LEFT JOIN
(select n.firstname, n.lastname, n.birth, n.middlename) as
ON e.firstname = n.firstname, e.lastname = n.lastname, e.birth = n.birth, e.middlename = n.middlename
where e.municip=1234 and e.year IN (2011,2010);
This doesn't work (obviously), and I guess I cannot run a JOIN without a unique id. (显然),这行不通,而且我想如果没有唯一的ID我就无法运行JOIN。 Any idea on how to sort this out?
关于如何解决这个问题的任何想法?
Maybe you're looking for this: 也许您正在寻找这个:
SELECT e.firstname, e.lastname, e.birth, e.middlename,
e.income - n.income as incomediff
FROM incomedata e
LEFT JOIN incomedata n
ON e.firstname = n.firstname
AND e.lastname = n.lastname
AND e.middlename = n.middlename
AND e.birth = n.birth
WHERE e.municip = 1234
AND e.year = 2011
AND n.year = 2010
There's nothing wrong with doing JOINs on multiple fields, you just have to AND
the conditions together instead of separating by commas like in your post. 有没有错,做JOIN的多个字段,你只需要
AND
条件在一起,而不是在您的文章喜欢用逗号分隔。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.