Categories
SQL / MySQL Tips and Tutorials

MySQL: Replace Substring with Another String – the MySQL String Replace Function

To find / identify a part of an original string and replace it with another string in a SQL query with MySQL, use REPLACE(), the string replace function.

For example, you may want to replace all spaces in one of the varchar or text fields to a single dash:

UPDATE `table` SET `old_field` = REPLACE(`old_field`, ' ', '-')

Or, you create a new column / field out of the old one and:

UPDATE `table` SET `new_field` = REPLACE(`old_field`, ' ', '-')
Categories
SQL / MySQL Tips and Tutorials

MySQL: Deleting Duplicate Rows or Records | Selecting Unique Rows into A New Table

One of the principles of data integrity is to reduce data redundancy as much as possible and wipe out any duplicate database table entries. Another reason for this is that you need to add unique indexing to one or a group of table columns and you need the one column or combination of columns to be unique through out all the rows of the table.

There are basically 2 ways to achieve that:

  1. Identifying duplicate entries, leaving just one of them and deleting the rest.
  2. Identifying only unique table rows and collect them into a new table.

I’ll just go ahead with the 2nd approach.

There are in turn 2 ways to accomplish this task, namely SELECT DISTINCT … FROM … and SELECT … FROM GROUP BY …

SELECT DISTINCT … FROM …

Rather intuitively, this SQL query selects all distinct combination of columns from a table and leaving out duplicate ones:

CREATE TABLE new_orders SELECT DISTINCT receipt, quantity, price FROM orders

One thing to note is that you have to leave out the original primary key such as ID so that all rows are only compared for uniqueness on necessary data fields. It is after this creation query is successfully completed when you add a primary key indexing column to the new_orders table.

SELECT … FROM GROUP BY …

GROUP BY clause is usually used for statistical functions of MySQL such as COUNT() or AVG() to only take a distinguished group of columns into calculation. In this situation, however, it comes handy that it inherently excludes multiple table records with the same value in the specified column and includes just one of them in the returned results.

For example, if you need to include only those records from the old table orders that’s with distinct values in column receipt:

CREATE TABLE new_orders SELECT * FROM orders GROUP BY receipt

Now all the records in the new table new_orders are with distinctly unique receipt values.

Depending on whether the original records are referenced by other tables via ID, you can regenerate the ID or leave them in the new table new_orders.

Categories
Affiliate Programs Domains My Personal Reviews

Dynadot Affiliate Program Review – Not Worth the While

I joined Dynadot at the commencement of .tel domains and tried my luck to get some domainers to register a domain with them.

Maybe it’s because they just don’t care about expanding via affiliates, the system might be a little buggy or not so affiliate friendly in that once the referred friend signed up an account with them, even if they have clicked through your affiliate link to register the domains at Dynadot, the newly registered domain and its commission will not be credited to you at all.

Therefore, make sure you send your friend or site visitors the affiliate link before they register the account with Dynadot. So, basically, they should register their account through your affiliate link. The other way won’t work for sure, but still I’m not sure if it works in this way.

Feels a little fishy with Dynadot domain registration affiliate program and totally not worth the while.

Categories
Affiliate Programs Make Money Online

Build Simple Amazon Affiliate Text Links with just ASIN (10 digit ISBN) and Your Amazon Associate Tracking ID

Amazon Affiliate system does a great job presenting you a variety of ways to make associate affiliate links so that you can take credit of their products that you have referred to others. Today I messed around a little bit with their generators and finally come to a simple solution to generate affiliate links dynamically and simply with server side programming.

The simplest way to link to an Amazon product and take the referral credit is to use this link:

https://www.amazon.com/dp/0470633956?tag=maawe-20

Wherein 0029724600 is the ASIN (equally the 10-digit ISBN) of the product (books!) and maawe-20 is my amazon affiliate tracking ID.

Or this:

https://www.amazon.com/gp/product/0470633956?tag=maawe-20

These would come quite handy when you build a book site with thousands of theme books, possibly on the same subject, and, you have a database of the books that contain the exact ISBN number for each of the book records. Now that you know amazon affiliate links can be built this way, it’s nothing more than a breeze to just insert the 10-digit ISBN number as ASIN into the URL template with your own affiliate ID.

Categories
SQL / MySQL Tips and Tutorials

1 tip about ALTER TABLE xxx ADD INDEX yyy

Proper indexing can work wonders for large databases in query efficiency, it’s a compromise of disk storage in exchange for sorting speed. For databases with millions of records, indexing takes considerable amount of disk storage, and, AGONIZING lengths of time to create.

I recently compiled a database with one table mytable consisting of over 4 million records, as apparently I want some columns to be identifiable for each of the rows, I need indexing one or 2 of them to do that.

ALTER TABLE mytable ADD INDEX category

Wherein category is one of the columns by which every record in mytable is referenced to that of another table.

You know how long this short query took me?

4 hours. Much longer than the time it took me to do a unique indexing just before it, on the same table.

So there you go, a general indexing takes much much longer time to finish than unique indexing and primary key indexing.

But that’s not all of the tip today.

The tip is that, before you are inserting all the records one by one into that table which would result in a super huge set of rows, configure that particular column to be generally indexed.

This is no better solution than indexing it all after the table is fully populated though, in that with preexisting table indexes, it takes a bit longer to insert every record as MySQL has to write down redundant information every time it inserts a new row into the table.

But in terms of programmer experience, it’s much better than waiting 4 hours hoping for the indexing to succeed and fearing it would not. I’d rather spread the extra required time across each insertion of the records.

Categories
Hilariously Funny or Creepy Kavoir & Whatever

Largest U.S. Bankruptcies, 1980–Present

Just imagine, that much amounts of money just vaporized into thin air at a cause of nothing.

Company Bankruptcy date Total assets pre-bankruptcy (in millions)
Lehman Brothers Holdings, Inc. 9/15/2008 $691,063
Worldcom, Inc.1 7/21/2002 $103,914
Enron Corp.2 12/2/2001 $65,503
Conesco, Inc. 12/17/2002 $61,392
Pacific Gas and Electric Co. 4/06/2001 $36,152
Texaco, Inc. 4/12/1987 $34,940
Financial Corp. of America 9/9/1988 $33,864
Refco Inc. 10/17/2005 $33,333
IndyMac Bancorp, Inc. 7/31/2008 $32,734
Global Crossing Ltd. 1/28/2002 $30,185
Bank of New England Corp. 1/7/1991 $29,773
Calpine Corp. 12/20/2005 $27,216
New Century Financial Corp. 4/2/2007 $26,147
UAL Corp. 12/9/2002 $25,197
Delta Air Lines, Inc. 9/14/2005 $21,801
Adelphia Communications 6/25/2002 $21,499
MCorp. 3/31/1989 $20,228
Mirant Corp. 7/14/2003 $19,415
American Home Mortgage Investment Corp. 8/6/2007 $18,829
NTL, Inc. 5/8/2002 $16,834

1. Worldcom, Inc. assets taken from the audited annual report dated 12/31/2001.

2. The Enron assets were taken from the tax documents filed on 11/19/2001. The company has announced that the financials were under review at the time of filing for Chapter 11.

Categories
Hilariously Funny or Creepy My Personal Reviews Web Applications & Online Software

Google AdWords mistake (a bug?)

Naturally, you’d never expect an AdSense ad of a site to appear on itself. But this I just encountered, indicates an inherent *bug* of AdWords.

adwords bug

Or is it? But I’ll never understand otherwise. What’s the point? Maybe there’s an option for the AdWords advertiser to opt out of his advertisement listing being published on his very own site with AdSense on, but can’t Google just do it automatically. My other guess is that for a very large community site such as a popular forum, there are people (who are members of the community) who wants to advertise to the very audience (mostly members) of the site, sets up a campaign via AdWords targeting them and puts the ad exclusively on the site which is also targeted a part of it.

Anyway, I believe Google should automatically turn this off by default and let the advertiser himself to explicitly turn it on.

Categories
SQL / MySQL Tips and Tutorials

MySQL: INSERT INTO … SELECT … FROM … Selectively on Table Columns / Fields to Combine Multiple Tables into One

While you can always fulfill tasks such as creating a new table out of existing ones in MySQL by the help of PHP, it’d be much faster and way more efficient with native SQL queries.

Consider when you need to combine a few tables into one, more precisely, you need just certain fields / columns from each of the table and then piece them together into a new one. You can write a PHP script to do that: 1) read the original records from the tables, 2) insert them one by one with a loop. However the insanely better way is to utilize INSERT INTO … SELECT … FROM … a little more sophisticated as:

INSERT INTO new_table (col1, col2, col3) SELECT old_table_1.col2 as col1, old_table_2.col3 as col2, old_table_3.col1 as col3 FROM old_table_1, old_table_2, old_table_3
Categories
Coupons and Promo Codes Domains Hosting Tips & Deals My Personal Reviews

DreamHost Review: Pros and Cons plus Uptime Charts

I’ve been DreamHost since 2006 and that’s more than 3 years of hosting experience with them on a shared plan ($9.95 / month). For those who’d prefer scanning instead of reading, I’ll sum up my points in short lists:

Pros

  1. Affordable price with generous offerings in disk storage and monthly transfer.
  2. Full pack of features – everything you can imagine for LAMP websites, they are there for you to access.
  3. 10 years of operating their own servers makes them extremely technical and the support is always to the point by knowledgeable staff.
  4. Proprietary control panel enables you to do everything without a hassle. Could be a Con though, as it’s too easy to use and somewhat addictive, once you get used to it, you’d be reluctant to switch.
  5. A lot of features are only found on DreamHost such as control panel API, Google Apps (I love it!), Jabber IM, Media Streaming and much more that are all automated and even a novice hoster can take advantage of.
  6. Open discussion forums.
  7. Very well documented wiki specifically for their users with detailed instructions to do everything.

Cons

  1. The average uptime is approximately 99.7% for shared plans – just so so.
  2. Sometimes it’s rather slow accessing your site. Blame the server load and amount of websites hosted on a single server. You can feel that you are in a shared environment without a doubt.
  3. I’ve experienced from 1 minute to 20 hours of response time from DreamHost email support. On average, it’s roughly 5 hours.
  4. Their referral program does work and the 2nd tier 5% forever rewards may successfully lock you with them “forever” because once you leave DreamHost, you leave the lifetime stream of 5% referral rewards. Pretty bad ass, huh?

DreamHost Server Stats Charts

All charts and data are gathered and provided by basicstate.com who pings (Spenser from basicstate.com points out that it’s not just pinging but also a full HTTP pull of the page) one of my sites hosted with DreamHost (shared plan) at 15-minute intervals.

DreamHost Uptime (shared)

dreamhost uptime chart

DreamHost Average Response Time (shared)

dreamhost average response time

DreamHost Total Response Time (shared)

dreamhost total response time

Conclusion

DreamHost (I can only speak of their shared plans which I have experience with) is absolutely worth the money if you are hosting hobbyist or personal sites that are not so mission critical. As a result of their sophisticated technical background, the hosting environment gives you as much freedom as you need yet balanced well in security and overall server well-being. It’s a paradise for web developers and non-technical users at the same time.

I’d especially recommend DreamHost to beginner webmasters for its affordable price and pack of features – you literally can’t find anything that’s offered by competitive hosting companies not in DreamHost’s menu. And, while you can do everything from the control panel, you can still utilize SSH to do advanced stuff. That said, the biggest drawback of DreamHost becoming a fantastic host is the reliability and performance issue that haunts its shared users.

DreamHost will be a terrific journey for you to learn and grow until you have to find a more solid pie to ensure your websites. I might go with their Private Server (Their way of VPS, a little more pricey than their shared plans in my opinion) plans soon, but with the same amount of money why don’t I go with another hosting company and try them out and in addition, to have a few different C-class IPs for the sake of SEO. 😉

Shameless Self Promotion: $60 DreamHost Coupon Code

Well, if you think my review is helpful and feel like going ahead, I’d very much appreciate it if you could use coupon CASH60 to sign up at DreamHost. It gives you an instant $60 discount with all yearly plans.

Here are some additional reviews if you would like to read more about DreamHost.

Categories
SQL / MySQL Tips and Tutorials

MySQL: Insert if doesn’t exist otherwise update the existing row

A good practice of increasing SQL efficiency is to reduce the number of separate queries as much as possible. In a situation where you want a row updated if it exists and inserted if it doesn’t may take a newbie MySQL developer to write 2 independent queries, namely:

  1. first, check if the row exists with “SELECT * FROM table WHERE …”
  2. second, if it exists, “UPDATE table SET …”; ir it doesn’t exist, “INSERT INTO table …”

thus loading the server with unnecessary burdens. Checking if a certain record already exists and decide between insert or update can be simply achieved by a single MySQL query:

REPLACE INTO table SET id = 42, foo = 'bar';

REPLACE INTO first checks if the row with id = 42 already exists, if it does, it deletes the row and insert the current one; if it doesn’t, it simply inserts the current record.

Another way is to use ON DUPLICATE KEY UPDATE clause in a paradigm such as INSERT … ON DUPLICATE KEY UPDATE to determine if the record to be inserted can cause a duplicate entry in a UNIQUE index or PRIMARY KEY index, if it doesn’t, it inserts it, if it does, the existing row is updated. The following two statements have the same results:

INSERT INTO table (a, b, c) VALUES (1, 2, 3) ON DUPLICATE KEY UPDATE c = c + 1;
// column ‘a’ is UNIQUE indexed, thus to prevent duplicate UNIQUE column values, the old row (whose a = 1) is updated with a new value of ‘c’
UPDATE table SET c = c + 1 WHERE a = 1;