(sql) how can I use count() method when data type is text?

后端 未结 3 1452
独厮守ぢ
独厮守ぢ 2021-01-16 22:35
select count(category) from list where category like \'action\'

above is a query i want to run. However, when I run that query, I am getting data

3条回答
  •  不思量自难忘°
    2021-01-16 23:14

    To answer the question in the title for future googlers you can use

    SELECT COUNT(CASE
                   WHEN some_text_column IS NOT NULL THEN 1
                 END)
    FROM   some_table
    

    In your case though (as pointed out in the comments by @hvd) the WHERE category LIKE 'action' predicate ensures that any NULL values will be excluded anyway so it is safe to replace it with COUNT(*)

    Moreover you almost certainly should not be using the text datatype for this. This is a deprecated datatype that was intended for holding LOB (Large Object) data over 8000 bytes.

    Strings like "action" definitely do not fit this description!

提交回复
热议问题