SQLite - 获取数据库中的行数 [英] SQLite - getting number of rows in a database

查看:135
本文介绍了SQLite - 获取数据库中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 max(id) 在我的表中获取一些行.当它返回 NULL - 如果表中没有行 - 我想返回 0.当有行时,我想返回 max(id) + 1.

我的行从 0 开始编号并自动增加.

这是我的声明:

SELECT CASE WHEN MAX(id) != NULL THEN (MAX(id) + 1) ELSE 0 END FROM words

但它总是给我返回 0.我做错了什么?

解决方案

如果你想使用 MAX(id) 而不是计数,在阅读 Pax 的评论后,下面的 SQL 会给你你想要的

>

SELECT COALESCE(MAX(id)+1, 0) FROM words

I want to get a number of rows in my table using max(id). When it returns NULL - if there are no rows in the table - I want to return 0. And when there are rows I want to return max(id) + 1.

My rows are being numbered from 0 and autoincreased.

Here is my statement:

SELECT CASE WHEN MAX(id) != NULL THEN (MAX(id) + 1) ELSE 0 END FROM words

But it is always returning me 0. What have I done wrong?

解决方案

If you want to use the MAX(id) instead of the count, after reading the comments from Pax then the following SQL will give you what you want

SELECT COALESCE(MAX(id)+1, 0) FROM words

这篇关于SQLite - 获取数据库中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆