2

我有一个名为的表tblCar,其中有ID,CarNumber和其他 32 个字段。填充了 32 列,s1,s2,s3,.......,s32因为它们专门用于备注是否适用于可用 (A)、预订 (B) 和已售出 (S) 的 SEATS。

我想从该数据库计算可用座位、预订座位和售罄座位。我应该如何编写查询?

无论如何要从 stringS和数据库BA的 32 个不同的列中计算?

我想展示的是这样的..... S = 20, B = 10, A = 2;

SqlCommand cmd = new SqlCommand(
  "Select count(*) 
   FROM tblCar 
   WHERE s1= 'S' or s2= 'S' or s3= 'S' 
   or s4= 'S' or s5= 'S' or s6= 'S' 
   or s7= 'S' or s8= 'S' or s9= 'S' 
   or s10= 'S' or s11= 'S' or s12= 'S' 
   or s13= 'S' or s14= 'S' or s15= 'S' 
   or s16= 'S' or s17= 'S' or s18= 'S' 
   or s19= 'S' or s20= 'S' or s21= 'S' 
   or s22= 'S' or s23= 'S' or s24= 'S' 
   or s25= 'S' or s26= 'S' or s27= 'S' 
   or s28= 'S' or s29= 'S' or s30= 'S' 
   or s31= 'S' or s32= 'S' ", con
); 
count += cmd.ExecuteNonQuery(); 

这就是我为“已售”字段工作的方式。但它只显示计数 = -128。我确实将计数初始化为 0

4

5 回答 5

3

试试这个 sql 查询:

SELECT SUM(CASE WHEN State = 'S' THEN 1 ELSE 0 END) AS Sold
, SUM(CASE WHEN State = 'B' THEN 1 ELSE 0 END) AS Booked
, SUM(CASE WHEN State = 'A' THEN 1 ELSE 0 END) AS Available
FROM 
    (SELECT S1, S2, S3, S4, S5, S6
    FROM Seats) s
UNPIVOT
    (State FOR Seat IN (S1, S2, S3, S4, S5, S6)) AS rows;

这里SQL Fiddle

在示例中,我只使用了 6 列,但在您的情况下,您需要为所有 32 列设置,但只使用一次

C#代码:

using SqlConnection conn = new SqlConnection(yourConnectionString)
{
    StringBuilder query = new StringBuilder();
    query.AppendLine("SELECT SUM(CASE WHEN State = 'S' THEN 1 ELSE 0 END) AS Sold");
    query.AppendLine(", SUM(CASE WHEN State = 'B' THEN 1 ELSE 0 END) AS Booked");
    query.AppendLine(", SUM(CASE WHEN State = 'A' THEN 1 ELSE 0 END) AS Available");
    query.AppendLine("FROM ");
    query.AppendLine("(SELECT S1, S2, S3, S4, S5, S6, S7, S8");
    query.AppendLine("(,S9, S10, S11, S12, S13, S14, S15, S16");
    query.AppendLine("(,S17, S18, S19, S20, S21, S22, S23, S24");
    query.AppendLine("(,S25, S26, S27, S28, S29, S30, S31, S32");
    query.AppendLine("FROM Seats) s");
    query.AppendLine("UNPIVOT");
    query.AppendLine("(State FOR Seat IN (S1, S2, S3, S4, S5, S6, S7, S8");
    query.AppendLine("(,S9, S10, S11, S12, S13, S14, S15, S16");
    query.AppendLine("(,S17, S18, S19, S20, S21, S22, S23, S24");
    query.AppendLine("(,S25, S26, S27, S28, S29, S30, S31, S32)) AS rows;");

    conn.Open();
    using SqlCommand command = new SqlCommand(query.ToString(), conn)
    {
        DataTable data = new DataTable();
        data.Load(command.ExecuteReader());
        //then get your values
        Int32 avialable = 0;
        Int32 booked= 0;
        Int32 sold = 0;
        if(data.Rows.Count > 0)
        {
            available = (Int32)data(0)("Available");
            booked = (Int32)data(0)("Booked");
            sold = (Int32)data(0)("Sold");
        }           
    }
}
于 2013-08-30T17:07:12.090 回答
0

你有一个非常讨厌的表结构,我会考虑改变它,但有了你所拥有的,你可以这样做:

    SELECT SUM(CASE s1 WHEN 'S' THEN 1 ELSE 0 END) as sold,
     SUM(CASE s1 WHEN 'A' THEN 1 ELSE 0 END) as available,
     SUM(CASE s1 WHEN 'B' THEN 1 ELSE 0 END) as booked,
   FROM tblCar 
   UNION
    SELECT SUM(CASE s2 WHEN 'S' THEN 1 ELSE 0 END),
     SUM(CASE s2 WHEN 'A' THEN 1 ELSE 0 END),
     SUM(CASE s2 WHEN 'B' THEN 1 ELSE 0 END),
   UNION
    ... Repeat the above for each field.... yuck!...
   UNION
   SELECT SUM(CASE s32 WHEN 'S' THEN 1 ELSE 0 END),
     SUM(CASE s32 WHEN 'A' THEN 1 ELSE 0 END),
     SUM(CASE s32 WHEN 'B' THEN 1 ELSE 0 END)
于 2013-08-30T15:44:18.857 回答
0

如果您希望它返回一个值,您应该使用 ExecuteScalar。

SqlCommand cmd = new SqlCommand("Select count(*) FROM tblCar WHERE s1= 'S' or s2= 'S' or s3= 'S' or s4= 'S' or s5= 'S' or s6= 'S' or s7= 'S' or s8= 'S' or s9= 'S' or s10= 'S' or s11= 'S' or s12= 'S' or s13= 'S' or s14= 'S' or s15= 'S' or s16= 'S' or s17= 'S' or s18= 'S' or s19= 'S' or s20= 'S' or s21= 'S' or s22= 'S' or s23= 'S' or s24= 'S' or s25= 'S' or s26= 'S' or s27= 'S' or s28= 'S' or s29= 'S' or s30= 'S' or s31= 'S' or s32= 'S' ", con); 
count += (int)cmd.ExecuteScalar();
于 2013-08-30T15:40:36.733 回答
0

由于您要返回 1 个值,因此您应该调用.ExecuteScalar. Upi 还需要将返回值转换为 int。

count = Convert.ToInt32(sqlcmd01.ExecuteScalar());

来自 MSDN

执行查询,并返回查询返回的结果集中第一行的第一列。其他列或行将被忽略。

您只在寻找 1 个值,因此只需调用cmd.ExecuteScalar(); ExecuteNonQuery()并且ExecuteReader()对其他事物更好。(ExecuteNoNQuery()例如INSERTS

这个 SO Link很好地总结了这些差异。

于 2013-08-30T15:42:38.840 回答
-1

我个人的建议是重新设计表格。仅使用 3 列,“available”、“sold”和“Booking”。每列都保存和 int(也许是 bigint?),该 int 将保存所有具有该条件的座位的状态作为二进制代码。例如,如果座位 1、2、3 和 4 是唯一“已售出”的座位,则价值变为 1+2+4+8 = 17。如果只有 4 和 5 号座位被“售出”,则价值将为 8+16 = 24. 这样你就不必查询所有的列来找到售出的座位,你只需要查询'sold'列,它就会告诉你哪些座位被售出。

于 2013-08-30T18:23:17.597 回答