【问题标题】:C# code to check one columns data from table has exist in other tables用于检查表中一列数据的 C# 代码是否存在于其他表中
【发布时间】:2020-06-28 11:53:36
【问题描述】:

我想检查表A 中的一条记录以检查它是否存在于表B 中。 我计算表B中输入了多少数据。

例子:

   A                       B
------------------------------
John                     John
Joon                     Jorge
Jorge                    Joon
Elizabet                 Elizabet
Suzan                    Suzan
                         Elizabet
                         Joon
                         Suzan
                         John
                         Elizabet

答案应该是这样的:

John = 2
Joon = 2
jorge = 1
Elizabet = 3
Suzan = 2

我尝试过的:

using (ImportInvoiceMasterForm.ApplicationSqlConnection2 = new SqlConnection(MainForm.ApplicationDataBase))
            {
                ImportInvoiceMasterForm.ApplicationSqlConnection2.Open();
                using (ImportInvoiceMasterForm.ApplicationSqlCommand1 = new SqlCommand("SELECT * FROM TBL_Stock_Item", ImportInvoiceMasterForm.ApplicationSqlConnection2))
                using (ImportInvoiceMasterForm.ApplicationSqlDataReader1 = ImportInvoiceMasterForm.ApplicationSqlCommand1.ExecuteReader())
                {
                    while (ImportInvoiceMasterForm.ApplicationSqlDataReader1.Read())
                    {
                        ImportInvoiceMasterForm.GetImportQuantity = 0;
                        #region Get Import Quantity
                        using (ImportInvoiceMasterForm.ApplicationSqlConnection1 = new SqlConnection(MainForm.ApplicationDataBase))
                        {
                            ImportInvoiceMasterForm.ApplicationSqlConnection1.Open();
                            using (ImportInvoiceMasterForm.ApplicationSqlCommand = new SqlCommand("SELECT * FROM TBL_Import_Items WHERE ImportItemName='" + ImportInvoiceMasterForm.ApplicationSqlDataReader1.GetString(1) + "'", ImportInvoiceMasterForm.ApplicationSqlConnection1))
                            using (ImportInvoiceMasterForm.ApplicationSqlDataReader = ImportInvoiceMasterForm.ApplicationSqlCommand.ExecuteReader())
                            {
                                while (ImportInvoiceMasterForm.ApplicationSqlDataReader.Read())
                                {
                                    GetImportQuantity += double.Parse(ImportInvoiceMasterForm.ApplicationSqlDataReader.GetValue(4).ToString());
                                }
                            }
                        }
                        #endregion Get Import Quantity
                    }
                }
            }

【问题讨论】:

  • 你为什么不只在查询中这样做?我建议你使用存储过程并在那里编写逻辑

标签: c# sql


【解决方案1】:

这看起来像一个聚合。一种方法是join/group by:

select a.name, count(b.name)
from a left join
     b
     on a.name = b.name
group by a.name;

left join 允许0 的计数。

如果名称可以在a 中重复,那么关联子查询是更好的解决方案(假设您希望在a 中保留重复项):

select a.*, (select count(*) from b where b.name = a.name) as cnt
from a;

【讨论】:

    【解决方案2】:
    Select A.name, Count(B.name) As Num
    FROM A join B 
    Where B.name=A.name
    Group By A.name
    

    这将为您提供所需的解决方案

    【讨论】:

      猜你喜欢
      • 2017-10-28
      • 1970-01-01
      • 2022-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      相关资源
      最近更新 更多