SQL查询中的等式

SQL查询中的等式,sql,Sql,我试图在我们的数据库中识别重复数据 如果某人的第一个座位是1,最后一个座位是3,这意味着他们应该有x3个座位 所以我想运行一个查询,其中first_seat-last_seat+1=num_seats。我的查询有什么问题吗 select acct_id, event_id, event_name, section_name, row_name, first_seat, last_seat, num_seat from dba.v_event where first_seat - last_sea

我试图在我们的数据库中识别重复数据

如果某人的第一个座位是1,最后一个座位是3,这意味着他们应该有x3个座位

所以我想运行一个查询,其中first_seat-last_seat+1=num_seats。我的查询有什么问题吗

select acct_id, event_id, event_name, section_name, row_name, first_seat, last_seat, num_seat
from dba.v_event
where first_seat - last_seat +1 != num_seat

你的方程式是倒转的。它应该是
最后一个座位-第一个座位+1

在您的示例中,
first_seat=1
last_seat=3

first_seat - last_seat + 1 = 1 - 3 + 1 = -1
last_seat - first-seat + 1 = 3 - 1 + 1 = 3
如果您想允许按任意顺序列出座椅,可以使用
ABS()
获取差值的绝对值:

ABS(last_seat - first_seat) + 1

你的方程式是倒转的。它应该是
最后一个座位-第一个座位+1

在您的示例中,
first_seat=1
last_seat=3

first_seat - last_seat + 1 = 1 - 3 + 1 = -1
last_seat - first-seat + 1 = 3 - 1 + 1 = 3
如果您想允许按任意顺序列出座椅,可以使用
ABS()
获取差值的绝对值:

ABS(last_seat - first_seat) + 1

我想您的SQL server将返回一条错误消息,其中包含一些有用的信息。为什么不先看看呢?你得到了什么结果?他们怎么了?你用的是什么关系数据库?MySQL、SQL Server、Oracle、SQLite?请更正问题标签,以显示这一点。如果NUMNECATS可以从其他数据中派生出来,请考虑删除NuffiS座椅。它是冗余的并且容易出错。或者这就是您的意图,您正在验证假设?我想您的SQL server将返回一条错误消息,其中包含一些有用的信息。为什么不先看看呢?你得到了什么结果?他们怎么了?你用的是什么关系数据库?MySQL、SQL Server、Oracle、SQLite?请更正问题标签,以显示这一点。如果NUMNECATS可以从其他数据中派生出来,请考虑删除NuffiS座椅。它是冗余的并且容易出错。或者这就是你的意图,你在验证这个假设?