0

假设我在 SQL “abc”中有一个表,其中有一列“数字”,该列包含 (1,2,3,4) 等。

在我的第二个表“xyz”中,我有一列“数字”,该列包含 (1,2,3,4,5,6,7,8,9)。

现在我想比较这两者并在第三个表中插入相等的数据。那么我该怎么做呢?

代码 :

string str = "SELECT     Invoice_Details.PGI_ID, PARTY_BOOKING_DETAILS.PGI_ID AS abc ";
str += "FROM         PARTY_BOOKING_MAIN INNER JOIN ";
str += "    PARTY_BOOKING_DETAILS ON PARTY_BOOKING_MAIN.PBM_ID = PARTY_BOOKING_DETAILS.PBM_ID CROSS JOIN ";
str += "          Invoice_Details where PARTY_BOOKING_MAIN.PM_ID = 1 ";

SqlConnection con = new SqlConnection("data source = .; database = ePartyDatabase01; integrated security = true");
con.Open();
SqlCommand cmd1 = new SqlCommand("update Invoice_Details set [status] = @a", con);
SqlCommand cmd = new SqlCommand(str, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
    if (dr["PGI_ID"].ToString() == dr["abc"].ToString())
    {

        cmd1.Parameters.AddWithValue("@a", 1);

    }
}

dr.Close();
cmd1.ExecuteNonQuery();

con.Close();
4

3 回答 3

0

您可能想要使用某种这样的查询:

insert into table3 (number)
select 
    t1.number
from abc t1
inner join xyz t2
    on t1.number = t2.number
于 2013-02-27T09:55:55.460 回答
0

我还没有测试过,但你可能甚至不需要 C#,试试这样的:

SELECT xyz.number
INTO third_table
FROM (
   SELECT t1.number
   FROM first_table t1 JOIN second_table t2 ON t1.number = t2.number
) AS xyz
于 2013-02-27T09:56:08.413 回答
0

也许是这样的:

INSERT INTO table3(number)
SELECT
    number
FROM
    xyz
WHERE NOT EXISTS
    (
        SELECT
            NULL
        FROM
            abc
        WHERE
            abc.number=xyz.number
    )
于 2013-02-27T09:56:18.923 回答