如何让LIKE和COUNT返回小于不在行中的值的行数?

时间:2022-09-07 09:16:39

For example:

SELECT COUNT(ID) FROM My_Table
WHERE ID < 

(SELECT ID FROM My_Table
WHERE ID LIKE '%4'
ORDER BY ID LIMIT 1)    

My_Table:

  X          ID    Y     
------------------------
|      |     A1  |     |
------------------------
|      |     B2  |     |
------------------------
|      |     C3  |     |
------------------------ -----Page 1
|      |     D3  |     |
------------------------
|      |     E3  |     |
------------------------
|      |     F5  |     |
------------------------ -----Page 2
|      |     G5  |     |
------------------------
|      |     F6  |     |
------------------------
|      |     G7  |     | -----Page 3

There is no data ending in 4 but there still are 5 rows that end in something less than "%4".

没有数据以4结尾,但仍有5行以小于“%4”的结尾。

However, in this case were there is no match, so SQLite only returns 0

但是,在这种情况下没有匹配,所以SQLite只返回0

I get it is not there but how do I change this behavior to still return number of rows before it, as if it was there?

我得到它不存在,但如何更改此行为仍然返回它之前的行数,就好像它在那里?

Any suggestions?

Thank You.

3 个解决方案

#1


1  

SELECT COUNT(ID) FROM My_Table
WHERE ID < (SELECT ID FROM My_Table
  WHERE SUBSTRING(ID, 2) >= 4
ORDER BY ID LIMIT 1)   

#2


1  

Assuming there is always one letter before the number part of the id field, you may want to try the following:

假设在id字段的数字部分之前总是有一个字母,您可能需要尝试以下操作:

SELECT COUNT(*) FROM my_table WHERE CAST(substr(id, 2) as int) <= 4;

Test case:

CREATE TABLE my_table (id char(2));

INSERT INTO my_table VALUES ('A1');
INSERT INTO my_table VALUES ('B2');
INSERT INTO my_table VALUES ('C3');
INSERT INTO my_table VALUES ('D3');
INSERT INTO my_table VALUES ('E3');
INSERT INTO my_table VALUES ('F5');
INSERT INTO my_table VALUES ('G5');
INSERT INTO my_table VALUES ('F6');
INSERT INTO my_table VALUES ('G7');

Result:

5

UPDATE: Further to the comment below, you may want to consider using the ltrim() function:

更新:继续下面的评论,您可能需要考虑使用ltrim()函数:

The ltrim(X,Y) function returns a string formed by removing any and all characters that appear in Y from the left side of X.

ltrim(X,Y)函数返回一个字符串,该字符串是通过从X的左侧删除Y中出现的任何和所有字符而形成的。

Example:

SELECT COUNT(*) 
FROM   my_table 
WHERE  CAST(ltrim(id, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') as int) <= 4;

Test case (adding to the above):

测试用例(添加到上面):

INSERT INTO my_table VALUES ('ABC1');
INSERT INTO my_table VALUES ('ZWY2');

New Result:

7

#3


1  

In MySQL that would be:

在MySQL中将是:

SELECT  COUNT(ID)
FROM    My_Table
WHERE   ID < 
        (
        SELECT  id
        FROM    (
                SELECT  ID
                FROM    My_Table
                WHERE   ID LIKE '%4'
                ORDER BY
                        ID
                LIMIT 1
                ) q
        UNION ALL
        SELECT  MAX(id)
        FROM    mytable
        LIMIT 1
        )  

#1


1  

SELECT COUNT(ID) FROM My_Table
WHERE ID < (SELECT ID FROM My_Table
  WHERE SUBSTRING(ID, 2) >= 4
ORDER BY ID LIMIT 1)   

#2


1  

Assuming there is always one letter before the number part of the id field, you may want to try the following:

假设在id字段的数字部分之前总是有一个字母,您可能需要尝试以下操作:

SELECT COUNT(*) FROM my_table WHERE CAST(substr(id, 2) as int) <= 4;

Test case:

CREATE TABLE my_table (id char(2));

INSERT INTO my_table VALUES ('A1');
INSERT INTO my_table VALUES ('B2');
INSERT INTO my_table VALUES ('C3');
INSERT INTO my_table VALUES ('D3');
INSERT INTO my_table VALUES ('E3');
INSERT INTO my_table VALUES ('F5');
INSERT INTO my_table VALUES ('G5');
INSERT INTO my_table VALUES ('F6');
INSERT INTO my_table VALUES ('G7');

Result:

5

UPDATE: Further to the comment below, you may want to consider using the ltrim() function:

更新:继续下面的评论,您可能需要考虑使用ltrim()函数:

The ltrim(X,Y) function returns a string formed by removing any and all characters that appear in Y from the left side of X.

ltrim(X,Y)函数返回一个字符串,该字符串是通过从X的左侧删除Y中出现的任何和所有字符而形成的。

Example:

SELECT COUNT(*) 
FROM   my_table 
WHERE  CAST(ltrim(id, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') as int) <= 4;

Test case (adding to the above):

测试用例(添加到上面):

INSERT INTO my_table VALUES ('ABC1');
INSERT INTO my_table VALUES ('ZWY2');

New Result:

7

#3


1  

In MySQL that would be:

在MySQL中将是:

SELECT  COUNT(ID)
FROM    My_Table
WHERE   ID < 
        (
        SELECT  id
        FROM    (
                SELECT  ID
                FROM    My_Table
                WHERE   ID LIKE '%4'
                ORDER BY
                        ID
                LIMIT 1
                ) q
        UNION ALL
        SELECT  MAX(id)
        FROM    mytable
        LIMIT 1
        )