简体   繁体   English

SQL将多行计为一个查询

[英]SQL Counting Multiple Rows as One Query

Can someone help me. 有人能帮我吗。 Here's what I'm trying to do. 这就是我想要做的。
I have a table: 我有一张桌子:

**tblColors**
id    color_name
1      red
2      blue
3      white
4      white
5      blue
6      red
7      blue
8      white
9      red
10     blue

For example I accept 3 user inputs which are: 例如,我接受3个用户输入,分别是:
1. red 2. blue 3. white 1.红色2.蓝色3.白色

I want to count how many sets of these 3-colors are present in my database. 我想计算数据库中存在多少套这3种颜色。 (red-blue-white) (红蓝白)
In my sample database the answer should be: 在我的示例数据库中,答案应该是:

**tblColors**
id    color_name
------------------>id 1-3 is my first set of (red-blue-white)
1      red
2      blue
3      white
------------------

4      white
5      blue

-------------------------->id 6-8 is my second set
6      red
7      blue
8      white
--------------------------
9      red
10     blue


I have 2 (red-blue-white) set in my database so the result should be: 2 我的数据库中设置了2个(红色-蓝色-白色),因此结果应为:2
Sorry the description of the problem is not that clear but I hope you get the picture. 抱歉,对问题的描述还不清楚,但希望您能理解。

SELECT COUNT(*)
FROM   tblColors T1
       LEFT JOIN tblColors T2
           ON T1.id = T2.id - 1
       LEFT JOIN tblColors T3
           ON T2.id = T3.id - 1
WHERE  T1.color_name + '-' +
       T2.color_name + '-' +
       T3.color_name = 'red-blue-white'

Fiddler Demo 提琴手演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM