简体   繁体   English

避免跨过两列的重量范围? MySQL的

[英]Avoid crossing weight range BETWEEN, with two columns? mysql

I have two columns: amountFrom , amountTo in table shipping 我有两列:表格shipping amountFromamountTo

Lets say I have these row data: 可以说我有这些行数据:

amountFrom | amountTo
-----------------------
0            15
16           30
31           50

Now I would like to add these three: 现在,我想添加以下三个:

amountFrom | amountTo
-----------------------
15           22 (should fail, already exist (crosses range))
18           25 (should fail, already exist)
55           76 (should pass)

How can I make a correct sql query, that will run for each row i would like to insert, that will check if the "range" is available? 如何进行正确的sql查询,该查询将针对我想插入的每一行运行,从而检查“范围”是否可用?

Example of what i tried 我尝试过的例子

SELECT id FROM shipping WHERE amountFrom >= 15 AND amountTo <= 22

Above query returns no rows, which it should (if it was a correct query) since we dont want to make a new row with 15 and 22, as it will cross existing weight ranges 上面的查询不返回任何行,应该返回(如果是正确的查询),因为我们不想使用15和22来创建新行,因为它将跨越现有的权重范围

You can try this (here with values 15 and 22) : 您可以尝试一下(此处的值为15和22):

INSERT INTO t (amountFrom, amountTo)
 SELECT 15, 22
 WHERE NOT EXISTS (SELECT 1 FROM t WHERE 22 >= amountFrom AND 15 <= amountTo);

You can check the affected-rows value to see wether or not the row was actually inserted. 您可以检查受影响的行的值以查看是否实际插入了该行。

You don't have to do three separate inserts. 您不必做三个单独的插入。 You can do them all at once (at least with the data in your query). 您可以一次完成所有操作(至少使用查询中的数据)。

The select statement to see which do not overlap is: 查看不重叠的选择语句是:

select t2.*
from table2 t2
where not exists (select 1
                  from table1 t1
                  where t1.amountFrom <= t2.amountTo and
                        t1.amountTo >= t2.amountFrom
                 );

Two ranges overlaps if one starts before the other ends, and the first ends after the other starts. 如果一个范围在另一个端点之前开始,而第一个范围在另一个端点之后开始,则两个范围重叠。

You put this into an insert as 您将此insert

insert into t1(amountFrom, amountTo)
    select t2.amountFrom, t2.amountTo
    from table2 t2
    where not exists (select 1
                      from table1 t1
                      where t1.amountFrom <= t2.amountTo and
                            t1.amountTo >= t2.amountFrom
                     );

EDIT: 编辑:

If you want to do this one row at a time and prevent overlap in new rows as well: 如果您想一次执行一次此行,并且还要防止新行重叠:

insert into t1(amountFrom, amountTo)
    select t2.amountFrom, t2.amountTo
    from (select XX as amountfrom, YY as amountTo
         ) t2
    where not exists (select 1
                      from table1 t1
                      where t1.amountFrom <= t2.amountTo and
                            t1.amountTo >= t2.amountFrom
                     );

This will do the insert one step at a time with the overlap logic. 这将使用重叠逻辑一次完成插入操作。

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

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