Skip to main content

Posts

How to change a MySQL database’s table prefix

Changing a database table prefix is easy and here’s the simple step-by-step guide! For WordPress installations, it’s essential! How to change a prefix 1. In your text editor, change database_name, old_prefix_ and new_prefix_ to the required values: SET @database = "database_name"; SET @old_prefix = "old_prefix_"; SET @new_prefix = "new_prefix_"; SELECT concat( "RENAME TABLE ", TABLE_NAME, " TO ", replace(TABLE_NAME, @old_prefix, @new_prefix), ';' ) AS "SQL"  FROM information_schema.TABLES WHERE TABLE_SCHEMA = @database; 2. Run the query in cPanel or PHPMyAdmin on your WordPress database 3. The output will be a series of SQL queries that will rename the tables for you 4. Run the output 5. Done!   How to add a prefix    If your database doesn’t have a prefix at all, follow the steps above but use the below query that’s been slightly modified fo

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