我的子表包含以下列: idSubs,LastName,FirstName,Phone,Email,Type
我的subProjects表包含以下列:idSubs,idProjects,PaidOutAmount
我正在尝试获取PaidOutAmount中显示的最小金额和最大金额。我还希望承包商姓氏这些金额都支付给了。
我运行以下内容:
Select min(PaidOutAmount) least, subs.LastName as PaidTo,
max(PaidOutAmount) max, subs.LastName as PaidTo
from subprojects, subs
where subprojects.idSubs = subs.idSubs
结果表是:
least PaidTo max PaidTo
126.00 Torrey 5650.55 Torrey
这就是我想要的,除了第一个" PaidTo"专栏是"托里"它应该是" Bassler"。在我的subProjects表中,最小金额是126.00,但idSubs是6.如果我去我的子表,6实际上是指具有Bassler姓氏的承包商。 我不知道自己做错了什么。
根据Jorge的回答,我尝试了以下方法:
Select subs.Lastname as PaidTo, PaidOutAmount as Value from subprojects,
subs where subprojects.idSubs = subs.idSubs and PaidOutAmount =
min(PaidOutAmount) or PaidOutAmount = max(PaidOutAmount)
在stackoverflow上的其他地方找到max对于where子句无效并且应该使用子查询后,我将其更改为:
Select subs.Lastname as PaidTo, PaidOutAmount as Value from subprojects,
subs where subprojects.idSubs = subs.idSubs and (select PaidOutAmount =
min(PaidOutAmount)or PaidOutAmount = max(PaidOutAmount))
这将生成一个包含所有子和所有PaidOutAmounts的表。换句话说,它不像我正在尝试那样只提取最小值和最大值。经过实验,我发现上述任何配置或变体只会产生相同的结果,就像我只是使用以下内容一样:
Select subs.Lastname as PaidTo, PaidOutAmount as Value from subprojects,
subs
可能是什么问题?
对于密集的抱歉,我只是没有看到问题。根据你的建议,我去了SQlFiddle。我修改了你建议的查询:
SELECT
subs.LastName AS PaidTo, subprojects.PaidOutAmount as Value
FROM subprojects AS S
INNER JOIN (
SELECT
idSubs
, MIN(PaidOutAmount) MinPaidOutAmount
, MAX(PaidOutAmount) MaxPaidOutAmount
FROM subprojects
GROUP BY
idSubs
) AS
subs ON S.idSubs = subs.idSubs
WHERE S.PaidOutAmount = subs.MinPaidOutAmount
OR S.PaidOutAmount = subs.MaxPaidOutAmount
我在SqlFiddle中添加的相关表格如下:
CREATE TABLE subs
(idSubs int(11) NOT NULL ,
LastName VARCHAR(20),
FirstName VARCHAR(15) ,
Phone VARCHAR(10) ,
Email VARCHAR(45) ,
Type VARCHAR(15),
PRIMARY KEY (`idSubs`));
CREATE TABLE subprojects (
idSubs int(11) NOT NULL ,
idProjects INT(11) NOT NULL,
PaidOutAmount DECIMAL(7,2) NULL DEFAULT NULL ,
PRIMARY KEY (`idSubs`, `idProjects`));
INSERT INTO SubProjects VALUES (1, 200, 755.23);
INSERT INTO SubProjects VALUES (6, 200, 126.00);
INSERT INTO SubProjects VALUES (8, 200, 2523.53);
INSERT INTO SubProjects VALUES (1, 300, 5650.55);
INSERT INTO SubProjects VALUES (6, 300, 750.19);
INSERT INTO SubProjects VALUES (7, 300, 2500.00);
INSERT INTO SubProjects VALUES (8, 100, 374.14);
INSERT INTO Subs VALUES
(1,'Torrey','Brian','5635435877','torrey@torrey.com', 'Carpenter');
INSERT INTO Subs VALUES (2, 'McDonough', 'Mark', '5635997115',
'McDonough@gmail.com', 'Carpenter');
INSERT INTO Subs VALUES (3, 'Basler', 'Bruce', '5635904457',
'BB@yahoo.com', 'Carpenter');
INSERT INTO Subs VALUES (4, 'ThePainter', 'Tyler', '5635800330',
'paintpros@gmail.com', 'Painter');
INSERT INTO Subs VALUES (5, 'ThePainter2', 'Adam', '5635642819',
'adampnt@gmail.com', 'Painter');
INSERT INTO Subs VALUES (6, 'A&G', 'Dean', '5635902919',
'dean12@agelectric.com', 'Electrician');
INSERT INTO Subs VALUES (7, 'AllSeasons', 'Dean', '5635431296',
'service@allseasons.com', 'Heating/Cooling');
INSERT INTO Subs VALUES (8, 'Gregory', 'Tom', '5634950997',
'tgregory@floorshowcompanies.com', 'Flooring');
INSERT INTO Subs VALUES (9, 'Jaeger', 'Kevin', '5635836677',
'kevin@jaegerplmb.com', 'Plumbing');
答案 0 :(得分:0)
要过滤您需要的聚合:
但是您的查询需要其他东西 - 子查询,类似这样:
SELECT
s.*
FROM subprojects AS S
INNER JOIN (
SELECT
idSubs
, MIN(PaidOutAmount) MinPaidOutAmount
, MAX(PaidOutAmount) MaxPaidOutAmount
FROM subprojects
GROUP BY
idSubs
) AS
subs ON S.idSubs = subs.idSubs
WHERE S.PaidOutAmount = subs.MinPaidOutAmount
OR S.PaidOutAmount = subs.MaxPaidOutAmount
答案 1 :(得分:0)
好吧,这个令人费解的混乱是我必须要做的,但实际上它可以获得我想要的信息。如果有人想告诉我如何做得更好,那就太棒了。
select
(select min(PaidOutAmount)
from subprojects) as min,
(Select Lastname
from subs, subprojects
where subs.idSubs = subprojects.idSubs
order by PaidOutAmount asc
limit 1) as PaidTo,
(Select max(PaidOutAmount) from subprojects) as Max,
(Select Lastname
from subs, subprojects
where subs.idSubs = subprojects.idSubs
order by PaidOutAmount desc
limit 1) as PaidTo
它产生了这个:
min PaidTo Max PaidTo
126.00 A&G 5650.55 Torrey
这也有效。
SELECT LastName,PaidOutAmount
FROM subs, subprojects
WHERE PaidOutAmount = (SELECT Max(PaidOutAmount)
FROM subprojects ) and subs.idSubs = subprojects.idSubs
or (PaidOutAmount = (SELECT Min(PaidOutAmount)
FROM subprojects ) and subs.idSubs = subprojects.idSubs )
LastName PaidOutAmount
Torrey 5650.55
A&G 126.00