gpt4 book ai didi

mysql - 查看帮助。从 3 个表中提取数据

转载 作者:行者123 更新时间:2023-11-30 23:24:58 25 4
gpt4 key购买 nike

好的,我需要一些关于这个主题的重要帮助。这就是我需要 View 做的事情。需要取Attendance表中DKP_Change列的Sum

 SELECT SUM(a.DKP_Change) FROM Attendance AS a GROUP BY Name

从角色表中添加初始DKP的值

 SELECT b.Inital_DKP FROM Characters AS b GROUP BY Name

减去 raid drops tabe 成本的总和

 SELECT SUM(c.Cost) FROM Raid_Drops AS c GROUP BY Name

我对 VIEWS 的想法完全陌生,我不确定从哪里开始, View 的名称应该是 DKP,列应该是 Name 和 Total_DKP,其中总 dkp 是根据上面的选择计算的声明。

这是所有 3 个表的创建。

CREATE TABLE `Attendance` (
`Date` date NOT NULL,
`Name` varchar(20) NOT NULL,
`Hours` int(11) NOT NULL,
`Penalty` float NOT NULL,
`Rank` set('Raider','Core','Elite') NOT NULL,
`Rate` int(11) NOT NULL,
`DKP_Change` float NOT NULL,
`RecordNumber` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`RecordNumber`)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=latin1

CREATE TABLE `Characters` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(25) NOT NULL,
`Class` varchar(25) NOT NULL,
`Spec` varchar(25) NOT NULL,
`Position` set('Healer','Tank','DPS') NOT NULL COMMENT 'Healer, Tank, or DPS',
`Usable` set('Cloth','Mail','Plate') NOT NULL COMMENT 'Type of Usable Armor? Cloth, Mail, Or Plate',
`Primary Stat` set('Agility','Strength','Intellect','Healer','Tank') NOT NULL COMMENT 'Used for Sorting Only(ie dps trinket with agility strength dps not eligible)',
`Initial_DKP` int(11) NOT NULL COMMENT 'DKP given at the start of current tier.',
`Total_DKP` int(11) NOT NULL COMMENT 'Huge Complicated Mess.',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1

CREATE TABLE `Raid_Drops` (
`Record Number` int(11) NOT NULL,
`Date` date NOT NULL,
`Name of Item` varchar(25) NOT NULL,
`Item Slot` enum('Main Hand','Off Hand','Head','Neck','Shoulder','Back','Chest','Wrist','Hands','Waist','Legs','Feet','Ring 1','Ring 2','Trinket 1','Trinket 2') NOT NULL,
`Player_Name` varchar(25) NOT NULL,
`Cost` float NOT NULL,
PRIMARY KEY (`Record Number`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

最佳答案

你可以

  • 在子选择中加入三个表,根据 Name 分组
  • 对每个子选择的结果执行计算

我唯一不完全清楚的部分是 Characters 中的名称是否唯一。如果是,您可以删除该组。否则,AVG 可能会给您带来意想不到的结果。

SQL语句

SELECT sumA
, initialB
, sumC
, sumA + initialB - sumC
, a.Name
FROM (
SELECT Name, SUM(DKP_Change) AS sumA
FROM Attendance
GROUP BY Name
) AS a
INNER JOIN (
SELECT Name, Inital_DKP AS initialB
FROM Characters
) AS b ON a.Name = b.Name
INNER JOIN (
SELECT Name, SUM(Cost) AS sumC
FROM Raid_Drops
GROUP BY Name
) AS c ON c.Name = b.Name

关于mysql - 查看帮助。从 3 个表中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13814120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com