0

我有带有字段的表 TClientsPayment

idcontract,idclient,last_paid_time,paid,left_to_pay

我需要将此表中的数据插入到另一个带有字段的表 TClientsPaymentsHistory

idcontract,idclient,last_paid_time,paid,late

late字段在哪里bitlate可以是 0 或 1。这取决于客户是否在 25.xx.xxxx 之前付款。如果不是late= 1。

如果我查询

insert into TClientPaymentHistory
select idcontract,
       idclient,
       last_paid_time,
       paid
from TClientsPayments

那么如何late在这个插入查询中设置字段呢?

4

2 回答 2

2

Use the CASE expression in the SELECT clause, here is a pseudo code of how you can do this:

insert into TClientPaymentHistory
select idcontract,
       idclient,
       last_paid_time,
       paid,
       CASE 
         WHEN DATEDIFF(day, last_paid_time, 'tell date here') <= something THEN 0
         ELSE 1
       END AS late     
from TClientsPayments

Since you didn't specify what RDBMS you are using, you will need to determine the correct way to get the date difference depending on the SQL dialect you are using.

于 2012-11-11T15:31:27.077 回答
2

Something like this, although you'll need to be clearer about the condition:

Insert into TClientPaymentHistory (
  idcontract, idclient, last_paid_time, paid, late
)
Select
   idcontract,
   idclient,
   last_paid_time,
   paid,
   Case When last_paid_time < 25.xx.xxxx then 0 else 1 end
From
  TclientPayments
于 2012-11-11T15:31:44.710 回答