联接表以从其他表获取ID名称

时间:2018-08-31 11:46:25

标签: mysql sql

我有下表:

CREATE TABLE PaL (
    Event_Date DATE,
    Country CHAR(2),
    Category CHAR(255),
    Revenue INTEGER(255),
    Costs INTEGER(255)
);

INSERT INTO PaL 
(Event_Date, Country, Category, Revenue, Costs)
VALUES

("2017-01-31", "1", "1", "691.09816652375", "-173.071989376023"),
("2017-02-28", "1", "1", "8419.9977988914", "-7622.61265984317"),
("2017-03-31", "1", "1", "2018.80471444031", "-1498.76213884283"),
("2017-04-30", "1", "1", "8863.15663035884", "-7965.69268589649"),
("2017-05-31", "1", "1", "6838.4514829573", "-1088.70351845663"),
("2017-06-30", "1", "1", "2025.73421386331", "-483.454199185678"),
("2017-07-31", "1", "1", "5389.0163788639", "-2643.93624645182"),
("2017-08-31", "1", "1", "6238.85870250446", "-1985.9879371866"),
("2017-09-30", "1", "1", "2294.62451106469", "-1864.98271539745"),
("2017-10-31", "1", "1", "4141.2074159951", "-197.773961036073"),
("2017-11-30", "1", "1", "1456.17584217397", "-1018.54129047119"),
("2017-12-31", "1", "1", "3623.54984724091", "-745.715567286581"),

("2017-01-31", "1", "2", "5955.20947079185", "-4745.39564508682"),
("2017-02-28", "1", "2", "9555.29563511224", "-5729.82601329738"),
("2017-03-31", "1", "2", "5490.36170257556", "-925.286457266534"),
("2017-04-30", "1", "2", "7652.35548686073", "-7335.32532050594"),
("2017-05-31", "1", "2", "9102.38987703511", "-5724.92574170071"),
("2017-06-30", "1", "2", "1703.95901703023", "-1678.19547060803"),
("2017-07-31", "1", "2", "3679.60045104324", "-2095.94207835501"),
("2017-08-31", "1", "2", "6672.43210841331", "-3475.55411014914"),
("2017-09-30", "1", "2", "7718.7744220635", "-1252.75877307055"),
("2017-10-31", "1", "2", "6976.01564153854", "-509.991595559256"),
("2017-11-30", "1", "2", "4725.46976319905", "-2835.09460170927"),
("2017-12-31", "1", "2", "8390.84483147949", "-7476.83516162742"),


("2017-01-31", "2", "1", "939788.159047677", "-742666.846083707"),
("2017-02-28", "2", "1", "826418.514009279", "-702997.151099908"),
("2017-03-31", "2", "1", "775940.69563018", "-211238.971709086"),
("2017-04-30", "2", "1", "516829.583069596", "-407521.856789393"),
("2017-05-31", "2", "1", "635701.377748304", "-627829.016481388"),
("2017-06-30", "2", "1", "757852.95599751", "-740948.867522139"),
("2017-07-31", "2", "1", "154224.257732688", "-139805.456987081"),
("2017-08-31", "2", "1", "102035.465731255", "-100103.875992667"),
("2017-09-30", "2", "1", "880671.692714021", "-665324.083753931"),
("2017-10-31", "2", "1", "187868.653562564", "-105676.793254039"),
("2017-11-30", "2", "1", "994600.486892401", "-177382.899789215"),
("2017-12-31", "2", "1", "813824.90461202", "-132527.311010471"),

("2017-01-31", "2", "2", "661069.933966637", "-454778.427240679"),
("2017-02-28", "2", "2", "675942.334464692", "-254489.623313569"),
("2017-03-31", "2", "2", "473604.307973888", "-404226.047653847"),
("2017-04-30", "2", "2", "872018.822577053", "-348781.396359871"),
("2017-05-31", "2", "2", "718012.023481434", "-625306.312927362"),
("2017-06-30", "2", "2", "688487.679029354", "-584512.575888519"),
("2017-07-31", "2", "2", "690085.013711018", "-581753.771085971"),
("2017-08-31", "2", "2", "204473.88894161", "-172301.871771595"),
("2017-09-30", "2", "2", "516932.092423463", "-328002.737710081"),
("2017-10-31", "2", "2", "609355.245817292", "-323624.391366703"),
("2017-11-30", "2", "2", "313599.625504231", "-210253.020497022"),
("2017-12-31", "2", "2", "723573.681040319", "-107333.764977439");


CREATE TABLE Categories (
    ID CHAR(2),
    Name CHAR(255)
);

INSERT INTO Categories
(ID, Name)
VALUES

("1", "Apparel"),
("2", "Shoes");


CREATE TABLE Countries (
    ID CHAR(2),
    Name CHAR(255)
);

INSERT INTO Countries
(ID, Name)
VALUES

("1", "DE"),
("2", "US");

我使用以下查询从表PaL中获取数据:

Select Country, Category, sum(Revenue) as Revenue, sum(Costs) as Costs
FROM PaL
WHERE Event_Date BETWEEN "2017-01-01" AND "2017-12-31"
GROUP BY Country, Category

您还可以在sql fiddle here中找到表格。
到目前为止,所有这些工作都可以找到。


现在,我想将表CategoriesCountries连接到我的SQL查询中,因此它代替ID而不是Name在结果中显示Select a.Country, a.Category, sum(a.Revenue) as Revenue, sum(a.Costs) as Costs FROM PaL a JOIN Categories b ON a.PaL = b.Name JOIN Countries c ON a.PaL = c.Name WHERE Event_Date BETWEEN "2017-01-01" AND "2017-12-31" GROUP BY Country, Category 。因此,我将其更改为:

Unknown column 'a.PaL' in 'on clause'

但是,这给了我一个错误:html

您知道我的代码中哪里有错误吗?

2 个答案:

答案 0 :(得分:1)

您的表中没有Pal列。您需要为JOIN使用列名,而不是表名。见下文:

Select a.Country, a.Category, sum(a.Revenue) as Revenue, sum(a.Costs) as Costs
FROM PaL a
JOIN Categories b ON a.Category = b.ID
JOIN Countries c ON a.Country = c.ID
WHERE Event_Date BETWEEN "2017-01-01" AND "2017-12-31"
GROUP BY a.Country, a.Category

答案 1 :(得分:1)

您需要加入Console.WriteLine($"{b.Key}, {b.Value.Author}, {b.Value.Title}"); 表的COUNTRYCATEGORY列。 正如您所说的,这些列存储PaL分别为IDCOUNTRY,您需要如下修改连接条件。 同样在SELECT子句中,您需要从CATEGORY和COUNTRY表中获取值。

CATEGORY