How to get num results in mysqldb

I have the following query:

self.cursor.execute("SELECT platform_id_episode, title, from table WHERE asset_type='movie'")

Is there a way to get the number of results returned directly? I am currently doing inefficient:

r = self.cursor.fetchall()
num_results = len(r)
+4
source share
1 answer

If you really do not need results, * do not query MySQL for them; just use COUNT: **

self.cursor.execute("SELECT COUNT(*) FROM table WHERE asset_type='movie'")

Now you will return to one row with one column, whose value is the number of rows in which your other query will be returned.

, COUNT(*). A COUNT(platform_id_episode) , , NULL platform_id_episode; COUNT(*) - . ***


* ... , fetchall() , , .

** SQL, ; , , , ( ) .

*** - " * SELECT", , , . SELECT * , , , . SELECT COUNT(*) .

+3

All Articles