简体   繁体   English

如何在同一个 SELECT 中使用 2 个不同的条件相互替代

[英]How to use 2 different condition substitute for each other in the same SELECT

I'm having this issue Now, I'm having this table我有这个问题现在,我有这张桌子

Code代码 FirstNo第一没有 LastNo最后没有 Date_input日期输入
ABC1234 ABC1234 12 12 34 34 2022/06/06 2022/06/06
ABCD5678 ABCD5678 56 56 78 78 2022/07/07 2022/07/07
ABCD9111 ABCD9111 91 91 11 11 2022/07/07 2022/07/07
DEF1234 DEF1234 12 12 34 34 2022/06/06 2022/06/06
DEF5678 DEF5678 56 56 78 78 2022/07/07 2022/07/07

Then, I want to return 2 random number (these 2 number is from 00 to 99) which will be taken from front-end.然后,我想返回 2 个随机数(这 2 个数字是从 00 到 99),这将从前端获取。 Lets call them Random1 and Random2.让我们称它们为 Random1 和 Random2。 Now, when the query running, it will run from record 1 --> final现在,当查询运行时,它将从记录 1 运行 --> 最终

If whenever the FirstNo = Random1, then the next record will take the condition of FirstNo = Random2.如果每当FirstNo = Random1,那么下一条记录将采用FirstNo = Random2的条件。 Then, at the next record, it will return to get the condition of FirstNo = Random1.然后,在下一条记录时,它会返回得到FirstNo = Random1的条件。

For example, Random1 = 56, Random2 = 91, and the table as above比如Random1 = 56,Random2 = 91,上表如上

First record: FirstNo != 56, move to the next row.第一条记录:FirstNo != 56,移动到下一行。

Second record: FirstNo = 56.第二条记录:FirstNo = 56。

Third record: (Because of at the previous record, FirstNo equal Random1 (56), this record will get Random2 instead) FirstNo = 91.第三条记录:(因为在上一条记录中,FirstNo 等于 Random1 (56),所以这条记录会得到 Random2)FirstNo = 91。

Fourth record: Back to Random1, .....第四条记录:回到Random1,......

So, I'm figuring out something like:所以,我正在想办法:

SELECT * FROM CODE_DEX WHERE FirstNo = @Random1
SELECT * FROM CODE_DEX WHERE FirstNo = @Random2

I haven't have any idea how to joining these 2. I can create a column ISTRUE or something to Declare and Set in SQL, but I don't think I can use IF in the middle of the sql query.我不知道如何加入这两个。我可以创建一个列 ISTRUE 或其他东西来在 SQL 中声明和设置,但我认为我不能在 sql 查询中间使用 IF。

Thank you so much太感谢了

Based on stated requirement根据规定的要求

If the prior row value for FirstNo ordered by date is Random1 then return random2 for FirstNo如果按日期排序的 FirstNo 的前一行值为 Random1,则为 FirstNo 返回 random2

You would do that like this in SQL你会在 SQL 中这样做

SELECT Code, 
       CASE WHEN LAG(FirstNo,1) OVER (ORDER BY Date_Input) = Random1 
            THEN Random2
            ELSE FirstNo END AS FirstNo,
       LastNo,
       Date_input
FROM sometableyoudidnotname
ORDER BY Date_input

   

You can select:您可以选择:

  • next value, with the LEAD window function下一个值,带有LEAD窗口函数
  • previous value, with the LAG window function以前的值,具有LAG窗口功能

then filter out all rows which don't have " Random1 " and " Random2 " values in consecutive rows:然后过滤掉所有连续行中没有“ Random1 ”和“ Random2 ”值的行:

SET @Random1 = 56;
SET @Random2 = 91;

WITH cte AS (
       SELECT *,
              LAG(FirstNo)  OVER(ORDER BY Code, Date_input) AS PrevNo,
              LEAD(FirstNo) OVER(ORDER BY Code, Date_input) AS NextNo
       FROM tab
)
SELECT Code,
       FirstNo,
       LastNo,
       Date_input
FROM cte 
WHERE (FirstNo = @Random1 AND NextNo = @Random2)
   OR (FirstNo = @Random2 AND PrevNo = @Random1)

Check the demo here .此处查看演示。

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

相关问题 如何在一列中选择具有相同值但在另一列中所有列必须彼此不同的行 - How to select rows with same values in one column but all must be different between each other in another column 如何将同一表中不同条件的每个计数值分组为1个表 - How to group each count value of different condition in the same table into 1 table 如何从表中选择具有相同查询但条件不同的数据库中的数据? - How to select data from database with same query but different condition in a table? Mysql:SELECT multiple,每个选择条件不同 - Mysql : SELECT multiple with different condition of selection for each 如何 select 1 行每个条件 - How to select 1 row each condition SQL如何在不同的连接条件下使用相同的字段? - SQL how to use the same field with different join condition? 如何使用 Yii2 QueryBuilder 使用 select SAME 表中具有不同 Where 条件的相同列 - How to select SAME Column in SAME Table With Different Where Condition Using Yii2 QueryBuilder 在相同的select语句中计算具有不同条件的相同id列 - Count same id column with different condition in same select statement Select 行后的每个条件来自同一个表 myql - Select row after each condition from the same table myql 如何选择多个喜欢和其他条件的mysql - How to select multiple like and other condition mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM