SQLite db_num_rows VS count
#1

Hello!

What is better to use, this:
Код:
new DBResult:result = db_query(db, "SELECT COUNT(0) FROM players");
new count = db_get_field_int(result);
db_free_result(result);
or this:
Код:
new DBResult:result = db_query(db, "SELECT NULL FROM players");
new count = db_num_rows(result);
db_free_result(result);
to get count of results?


And another question: what, if I only want to know if a record just exists?

Thanks for your reply
Reply
#2

The first one is faster then the second one, because the first on just returns one row with the total players in the database. The second one returns more rows with data (in your query nothing, since you select NULL).
Reply
#3

Using SELECT COUNT(*) will count itself the number of rows that the table "players" contains and return it:
Код:
new DBResult:result = db_query(db, "SELECT COUNT(*) FROM players");
new count = db_get_field_int(result); //I think you don't really need this since SELECT COUNT(*) already returns an int
db_free_result(result);
Also replace SELECT NULL with SELECT *
Код:
new DBResult:result = db_query(db, "SELECT * FROM players"); //this gets every record of the table players
new count = db_num_rows(result); //counts the number of rows as int
db_free_result(result);
Between this two I'd personally use SELECT COUNT(*), instead of selecting everything from the table and then counting each row.
Quote:
Originally Posted by Quis
Посмотреть сообщение
And another question: what, if I only want to know if a record just exists?
Yes, you can check if a record exists without actually displaying it. For example, if you want to know if a player called John_Cena exists in the database:
Код:
SELECT 1 FROM players WHERE name = 'John_Cena'
This will return 1 if the record exists.
Reply
#4

Thanks for your answers

COUNT uses field name as parameter, so if you COUNT(*) you counting by all fields - I think it's a bad habit
COUNT(NULL), or COUNT(0) return just empty rows
Reply
#5

Quote:
Originally Posted by Quis
Посмотреть сообщение
Thanks for your answers

COUNT uses field name as parameter, so if you COUNT(*) you counting by all fields - I think it's a bad habit
COUNT(NULL), or COUNT(0) return just empty rows
It doesn't matter which on you use, but i prefer the COUNT(*) one, since NULL looks weird in SELECT.
Reply
#6

Quote:
Originally Posted by Quis
Посмотреть сообщение
Thanks for your answers

COUNT uses field name as parameter, so if you COUNT(*) you counting by all fields - I think it's a bad habit
COUNT(NULL), or COUNT(0) return just empty rows
No, not really..
COUNT(0) will return the same number as COUNT(*) or COUNT(1) or COUNT(100) or even COUNT(10000000)
They all do the same thing: COUNT(0) will be counting rows assigning 0 to each one of them, COUNT(1) will do the same assigning 1 and COUNT(*) will just count them as they are.
So they are pretty much the same, same stuff, same performance. Also COUNT(*) is not a bad habit, it's proper SQL syntax.
NB: there's a huge difference between NULL and 0 in SQL..
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)