Skip to main content

Posts

Selecting COUNT from MySQL table by first letter of a column

Method 1: SELECT count(*) total,     SUM(case when trim(last_name) like 'a%' then 1 else 0 end) A,     SUM(case when trim(last_name) like 'b%' then 1 else 0 end) B,     SUM(case when trim(last_name) like 'c%' then 1 else 0 end) C,     SUM(case when trim(last_name) like 'd%' then 1 else 0 end) D,     SUM(case when trim(last_name) like 'e%' then 1 else 0 end) E,     SUM(case when trim(last_name) like 'f%' then 1 else 0 end) F,     SUM(case when trim(last_name) like 'g%' then 1 else 0 end) G,     SUM(case when trim(last_name) like 'h%' then 1 else 0 end) H,     SUM(case when trim(last_name) like 'i%' then 1 else 0 end) I,     SUM(case when trim(last_name) like 'j%' then 1 else 0 end) J,     SUM(case when trim(last_name) like 'k%' then 1 else 0 end) K,     SUM(case when trim(last_name) like 'l%' then 1 else 0 end) L,     SUM(case when trim(last_name) like 'm%' then 1 e

Mysql regexp to allow numbers and '+'

First, use  NOT REGEXP  instead of  !=1 . To allow  +  at the start optionally, use  ^\\+? . Since  +  is a special character in regular expressions, it must be escaped with a backslash (which must be escaped with another backslash). You then need one or more digits ( [0-9]+ ), up to the end of the string  $ . $query="UPDATE table SET phone='NULL' WHERE phone NOT REGEXP '^\+?[0-9]+$'"; This will match anything that doesn't begin optionally with  + , followed by digits only. If you also need to permit hyphens and dots in the phone number, add them into the  []  character class: '^\\+?[0-9.-]+$'