1

我只能访问数据库的 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
4

2 回答 2

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>";
    }
}

它没有为答案提供完全正确的格式,但它告诉我我需要知道什么!

于 2013-08-30T21:30:53.043 回答
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 我的很草率)

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如果需要,您可以轻松删除别名...

于 2013-08-30T16:55:24.917 回答