Category Archives: SQL

WordPress – find and replace

I got some wordpress blogs (20+) running on some servers, and from time to time, i need to make some FIND&REPLACE on my posts…

This is the simple way! Directly on mysql.

UPDATE wp_posts SET post_content = REPLACE (  
post_content,  
'Item to replace here',  
'Replacement text here');

Please note the for security reasons the prefix wp_ is diferent… if you play by the rules you will need to see witch is is… wp-config.php has it.

mysql> UPDATE wpspt_posts SET post_content = REPLACE (
-> post_content,
-> 'static.mycdn.com/banners/',
-> 'static.mycdn.com/bnnrs/');
Query OK, 1477 rows affected (0.42 sec)
Rows matched: 10795 Changed: 1477 Warnings: 0

This is a small hack to ABP – Ad Blocker Plus…
Horray!

PostegresSQL HA

Authored jointly with Scott Mead, Sr. Architect OpenSCG and Architect of PostgresHA.

PostgresHA is Highly Available (HA) PostgreSQL.

 

PostgresHA Architecture

 

 

More in http://www.openscg.com/2013/04/postgresql-clustering/

BUT, Denis Lussier, C-Founder – CTO of EnterpriseDB says…

In general, we do NOT recommend running PostgresHA running synchronus replication. We recommend running PostgreSQL streaming repliction in Asynch mode. Monitoring and alerting must be configured if the slave node falls behind a certain tolerance of keeping up with the master.

 

SQL – LEFT OUTER JOIN adding returned null values

In a SQL with LEFT OUTER JOIN I had to sum some columns but some of them returned with NULL value and the result of it was a NULL.

So imagine that we want to add table1`.`columnA`to `table2`.`columnB`, we do something like this

SELECT *, (table1`.`columnA` + `table2`.`columnB`) AS total

and if some of them is NULL the total will be NULL.

What I had to do was this

SELECT *, (ifnull(`table1`.`columnA`,0 ) + ifnull(`table2`.`columnB`, 0)) AS total

And if you want to put it on codeigniter where is how I made it:

$this->db->select('*, (ifnull(`table1`.`columnA`,0 ) + ifnull(`table2`.`columnB`, 0)) AS total', FALSE);

Note the , FALSE, it has to be there.

$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.
http://ellislab.com/codeigniter/user-guide/database/active_record.html