我只能访问数据库的SELECT和WHERE子句。我没有能力做任何事情,只需填写SELECT和FROM之间以及WHERE之后的空格(“SELECT _ _ _ _ _ _ FROM TABLE WHERE _ _ _ _ _”)
在数据库中,有四个字段(实际上更多,但这些是相关的字段):“TShirt1”,“TShirt1Size”,“TShirt2”和“TShirt2Size”。
TShirt1和TShirt2都包含“RedCrew”,“BlueSlim”或“GreyV”中的一个
TShirt1Size和TShirt2Size都包含“S”,“M”,“L”或“XL”之一
我想知道是否可以创建一个返回每个组合总数的查询。 如果我没有两个字段,那就是
SELECT TShirt, TShirtSize, count(*) FROM BookingsTable WHERE 1 GROUP BY TShirt, TShirtSize
所以我会得到像
这样的结果RedCrew, L, 2
BlueSlim, M, 4
等。
有什么方法可以得到相同或类似的输出,但是结合了TShirt1和TShirt2的结果?
编辑:为混乱道歉。实际的表格类似于(未键入测试):create table tshirt (
TShirt_id int,
TShirt1 varchar(10),
TShirt1Size varchar(2),
TShirt2 varchar(10),
TShirt2Size varchar(2)
);
insert into tshirt values (1, 'RedCrew', 'XL', 'BlueSlim', 'M');
insert into tshirt values (2, 'BlueSlim', 'L', 'RedCrew', 'L');
insert into tshirt values (3, 'GreyV', 'L', 'BlueSlim', 'M');
insert into tshirt values (4, 'BlueSlim', 'M', '', '');
insert into tshirt values (5, '', '', 'GreyV', 'L');
insert into tshirt values (6, 'BlueSlim', 'S', 'BlueSlim', 'L');
我想要的结果集(如果我的数学是正确的)将是:
BlueSlim, S, 1
BlueSlim, M, 4
BlueSlim, L, 2
GreyV, L, 2
RedCrew, L, 1
RedCrew, XL, 1
答案 0 :(得分:1)
感谢您提出的各种建议,但这些建议都超出了我的范围。我最终得到了以下查询:
SELECT sum( if ((TShirt1='RedCrew' AND TShirt1Size='S') OR (TShirt2='RedCrew' AND TShirt2Size='S'), if ( (TShirt1='RedCrew' AND TShirt1Size='S') AND (TShirt2='RedCrew' AND TShirt2Size='S'), 2, 1), 0)) AS 'RedCrew S',
sum( if ((TShirt1='RedCrew' AND TShirt1Size='M') OR (TShirt2='RedCrew' AND TShirt2Size='M'), if ( (TShirt1='RedCrew' AND TShirt1Size='M') AND (TShirt2='RedCrew' AND TShirt2Size='M'), 2, 1), 0)) AS 'RedCrew M',
sum( if ((TShirt1='RedCrew' AND TShirt1Size='L') OR (TShirt2='RedCrew' AND TShirt2Size='L'), if ( (TShirt1='RedCrew' AND TShirt1Size='L') AND (TShirt2='RedCrew' AND TShirt2Size='L'), 2, 1), 0)) AS 'RedCrew L',
sum( if ((TShirt1='RedCrew' AND TShirt1Size='XL') OR (TShirt2='RedCrew' AND TShirt2Size='XL'), if ( (TShirt1='RedCrew' AND TShirt1Size='XL') AND (TShirt2='RedCrew' AND TShirt2Size='XL'), 2, 1), 0)) AS 'RedCrew XL',
sum( if ((TShirt1='BlueSlim' AND TShirt1Size='S') OR (TShirt2='BlueSlim' AND TShirt2Size='S'), if ( (TShirt1='BlueSlim' AND TShirt1Size='S') AND (TShirt2='BlueSlim' AND TShirt2Size='S'), 2, 1), 0)) AS 'BlueSlim S',
sum( if ((TShirt1='BlueSlim' AND TShirt1Size='M') OR (TShirt2='BlueSlim' AND TShirt2Size='M'), if ( (TShirt1='BlueSlim' AND TShirt1Size='M') AND (TShirt2='BlueSlim' AND TShirt2Size='M'), 2, 1), 0)) AS 'BlueSlim M',
sum( if ((TShirt1='BlueSlim' AND TShirt1Size='L') OR (TShirt2='BlueSlim' AND TShirt2Size='L'), if ( (TShirt1='BlueSlim' AND TShirt1Size='L') AND (TShirt2='BlueSlim' AND TShirt2Size='L'), 2, 1), 0)) AS 'BlueSlim L',
sum( if ((TShirt1='BlueSlim' AND TShirt1Size='XL') OR (TShirt2='BlueSlim' AND TShirt2Size='XL'), if ( (TShirt1='BlueSlim' AND TShirt1Size='XL') AND (TShirt2='BlueSlim' AND TShirt2Size='XL'), 2, 1), 0)) AS 'BlueSlim XL',
sum( if ((TShirt1='GreyV' AND TShirt1Size='S') OR (TShirt2='GreyV' AND TShirt2Size='S'), if ( (TShirt1='GreyV' AND TShirt1Size='S') AND (TShirt2='GreyV' AND TShirt2Size='S'), 2, 1), 0)) AS 'GreyV S',
sum( if ((TShirt1='GreyV' AND TShirt1Size='M') OR (TShirt2='GreyV' AND TShirt2Size='M'), if ( (TShirt1='GreyV' AND TShirt1Size='M') AND (TShirt2='GreyV' AND TShirt2Size='M'), 2, 1), 0)) AS 'GreyV M',
sum( if ((TShirt1='GreyV' AND TShirt1Size='L') OR (TShirt2='GreyV' AND TShirt2Size='L'), if ( (TShirt1='GreyV' AND TShirt1Size='L') AND (TShirt2='GreyV' AND TShirt2Size='L'), 2, 1), 0)) AS 'GreyV L',
sum( if ((TShirt1='GreyV' AND TShirt1Size='XL') OR (TShirt2='GreyV' AND TShirt2Size='XL'), if ( (TShirt1='GreyV' AND TShirt1Size='XL') AND (TShirt2='GreyV' AND TShirt2Size='XL'), 2, 1), 0)) AS 'GreyV XL'
WHERE 1;
我用PHP生成的:
$tees=array('RedCrew','BlueSlim','GreyV');
$sizes=array('S','M','L','XL');
foreach ($tees as $t)
{
foreach ($sizes as $s)
{
print "sum( if ((TShirt1='$t' AND TShirt1Size='$s') OR (TShirt2='$t' AND TShirt2Size='$s'), if ( (TShirt1='$t' AND TShirt1Size='$s') AND (TShirt2='$t' AND TShirt2Size='$s'), 2, 1), 0)) AS '$t $s',<br>";
}
}
它没有为答案提供完全正确的格式,但它告诉我我需要知道的内容!
答案 1 :(得分:0)
我真的不明白你想要什么......
create table #tshirt (
tshirt_id int,
tshirt_name varchar(10),
tshirt_color varchar(15),
tshirt_size varchar(2)
)
insert into #tshirt values (1, 'TShirt1', 'RedCrew', 'L')
insert into #tshirt values (2, 'TShirt1', 'RedCrew', 'L')
insert into #tshirt values (3, 'TShirt1', 'BlueSlim', 'M')
insert into #tshirt values (4, 'TShirt1', 'BlueSlim', 'M')
insert into #tshirt values (5, 'TShirt1', 'BlueSlim', 'M')
insert into #tshirt values (6, 'TShirt1', 'BlueSlim', 'M')
insert into #tshirt values (7, 'TShirt2', 'GreyV', 'S')
insert into #tshirt values (8, 'TShirt2', 'BlueSlim', 'XL')
SELECT tshirt_name, tshirt_color, tshirt_size, count(*) as ct
FROM #tshirt
GROUP BY tshirt_name, tshirt_color, tshirt_size
ORDER BY tshirt_name, tshirt_color, tshirt_size
--tshirt_name tshirt_color tshirt_size ct
--TShirt1 BlueSlim M 4
--TShirt1 RedCrew L 2
--TShirt2 BlueSlim XL 1
--TShirt2 GreyV S 1
SELECT tshirt_name, count(*) as ct
FROM #tshirt
GROUP BY tshirt_name
--tshirt_name ct
--TShirt1 6
--TShirt2 2
基于我的数据和架构:
SELECT tshirt_color, tshirt_size, count(*) as ct
FROM #tshirt
GROUP BY tshirt_color, tshirt_size
ORDER BY tshirt_color, tshirt_size
--tshirt_color tshirt_size ct
--BlueSlim M 4
--BlueSlim XL 1
--GreyV S 1
--RedCrew L 2
我认真建议您更改表格架构
create table #tshirt (
TShirt_id int,
TShirt1 varchar(100),
TShirt1Size varchar(100),
TShirt2 varchar(100),
TShirt2Size varchar(100)
);
insert into #tshirt values (1, 'RedCrew', 'XL', 'BlueSlim', 'M');
insert into #tshirt values (2, 'BlueSlim', 'L', 'RedCrew', 'L');
insert into #tshirt values (3, 'GreyV', 'L', 'BlueSlim', 'M');
insert into #tshirt values (4, 'BlueSlim', 'M', '', '');
insert into #tshirt values (5, '', '', 'GreyV', 'L');
insert into #tshirt values (6, 'BlueSlim', 'S', 'BlueSlim', 'L');
select tshirt_desc, tshirt_size, COUNT(*) as ct
from ( select
TShirt1 as tshirt_desc,
TShirt1Size as tshirt_size
from #tshirt
union all
select
TShirt2 as tshirt_desc,
TShirt2Size as tshirt_size
from #tshirt ) as tshirt
group by tshirt_desc, tshirt_size
order by tshirt_desc, tshirt_size
--tshirt_desc tshirt_size ct
-- 2
--BlueSlim L 2
--BlueSlim M 3
--BlueSlim S 1
--GreyV L 2
--RedCrew L 1
--RedCrew XL 1
试试这个..(我不得不使用在线格式化程序b / c mine非常草率)
SELECT tshirt1 AS TShirt,
tshirt1size AS TShirtSize,
(SELECT ct
FROM (SELECT tshirt,
tshirtsize,
Count(*) AS ct
FROM (SELECT tshirt1 AS TShirt,
tshirt1size AS TShirtSize
FROM #tshirt
UNION ALL
SELECT tshirt2 AS TShirt,
tshirt2size AS TShirtSize
FROM #tshirt) AS tshirt
GROUP BY tshirt,
tshirtsize) AS inner_t
WHERE inner_t.tshirt = outer_t.tshirt1
AND inner_t.tshirtsize = outer_t.tshirt1size) AS ct
FROM #tshirt AS outer_t
http://sqlfiddle.com/#!3/ec024/8/0
如果您需要,可以轻松删除outer_t
别名...