简体   繁体   English

使用外键更新值和表

[英]Update a value and a table with a foreign key

ticketId(**)票号(**) timeExpected预计时间 timeElapsed时间流逝
187 187 5 5 5 5
225 225 4 4 8 8
856 856 8 8 15 15
782 782 10 10 8 8

**primary key *foreign key **主键 *外键

id(**) ID(**) (*)ticketId (*)ticketId beyondTime超越时间
1 1 187 187 0 0
2 2 225 225 1 1
3 3 856 856 1 1
4 4 782 782 0 0

I have to know which ticket his out of time and I have this in mind and in my database but I can't figure it out with SQL.我必须知道他的哪张票过期了,我在我的数据库中记住了这一点,但我无法用 SQL 弄清楚。 I want to know when a ticket is out of time like the ticket number 225 is, I would like to update the other table with a binary 1 for "out of time" and 0 "good".我想知道一张票什么时候过期,就像票号 225 一样,我想用二进制 1 更新另一个表,表示“过期”,0 表示“好”。

I don't know if I can update the "beyondTime" table when I do "timeExpected - timeElapsed" in the first table when a ticket exceed the time expected.当票超过预期时间时,我不知道当我在第一个表中执行“timeExpected - timeElapsed”时是否可以更新“beyondTime”表。

First, let's simplify the problem you're looking a solution for:首先,让我们简化您正在寻找解决方案的问题:

Comparing two data field values for the rows in a table比较表中行的两个数据字段值

I believe that's basically what you're trying to do.我相信这基本上就是你想要做的。 And of course, you can make a decision based on the comparison and act accordingly like updating some values in another table if you want to.当然,您可以根据比较做出决定,并根据需要采取相应的行动,例如更新另一个表中的某些值。

But if all you're looking for is kind of a report, then all you need would be just a select statement equipped with the logic doing the comparison for you.但是,如果您要寻找的只是一份报告,那么您所需要的只是一个 select 语句,该语句配备了为您进行比较的逻辑。

You can use SQL Case Statement for this.您可以为此使用 SQL 案例声明。 Here is an easy guide if you want to take a look.如果您想看一下,这里有一个简单的指南。

The select statement you need would be like this:您需要的 select 语句如下所示:

select ticketId, (case when timeElapsed > timeExpected then 1 else 0 end) as beyondTime from tickets

The result would be like this:结果将是这样的:

在此处输入图像描述

Two things to remember:要记住两件事:

  1. Don't get disappointed by the negative points and keep asking questions:)不要对负面观点感到失望并继续提问:)
  2. Identify the issue you need help with and be specific with your question.确定您需要帮助的问题并具体说明您的问题。 I'd suggest you to update this question.我建议你更新这个问题。

Good luck!祝你好运!

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

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