Entries tagged "MySQL"

MySQL GROUP BY (Aggregate) Functions: A Prime Candidate

The Problem

A few days ago I was contacted by a colleague in need of some SQL help. He has a piece of athletic management software that tracks various metadata about teams. In his schedule table he tracks the host/opponent id and game scores, among other things. He needed an easy way to query for the number of wins, losses, and ties for each team (for all completed games). Following is the query he had constructed prior to contacting me:

SELECT id, host_id, host_score, opponent_id, opp_score,
(case
   when (host_score > opp_score) then host_id
   when (host_score < opp_score) then opponent_id
   else 0
end) as winner,
(case
   when (opp_score > host_score) then host_id
   when (opp_score < host_score) then opponent_id
   else 0
end) as loser,
(case
   when (opp_score = host_score) then opponent_id
   else 0 
end) as tie1,
(case
   when (opp_score = host_score) then host_id
   else 0
end) as tie2
FROM schedule
WHERE status = 'COM'

The query above gave him the winner and loser for each game (and whether or not the game resulted in a tie), however he needed an aggregate count of wins/losses/ties.

MySQL GROUP BY (Aggregate) Functions: SUM

I don't write SQL every day, however my instincts told me two things: 1) his solution looked much to complicated for what he was trying to accomplish (a sure sign that you might be doing something wrong), and 2) this sounded like a classic GROUP BY problem. After pulling out my trusty SQL book for reference, I constructed the following query and passed it along:

SELECT host_id, 
    SUM(host_id > opponent_id) AS wins, 
    SUM(host_id < opponent_id) AS losses, 
    SUM(host_id = opponent_id) AS ties 
FROM schedule 
WHERE status = 'COM' 
GROUP BY host_id

Conclusion

If you need aggregate data on fields in one of your database tables (AVG, COUNT, SUM, etc), your mind should immediately go to GROUP BY. It could save you time and headache.

MySQL Replace Rocks

We have a client who is having issues with character encoding.  They recently had a massive content migration and along with the migration came numerous character issues.  They moved from a character set of iso-8859-1 to UTF-8, and with multiple content editors copying/pasting from many different resources (pdf’s, Word, Web Pages, etc) they now have some cleanup to do.  We were recently asked if we could provide a report on all the pages where certain characters resided.  No problem.  We ran some SQL queries (most content resides in the database thankfully) that looked similar to the following:

SELECT p.id, p.title FROM `pages` p WHERE
p.content REGEXP '(¢|â|Ã)';

In the case of the above query, we’re just taking advantage of MySQL’s native REGEXP operator to check for the existence of any of the three characters (simplified for illustration) in a content block.  After running our SQL statement we quickly realized that the work necessary to clean this up would likely amount to at least a few weeks for our client.  With an additional few hours of work (after backing things up and testing, testing, testing) we were able to provide a solution that will take a matter of minutes to implement (if the client chooses to hire us to do so). Here is an abbreviated version of what we came up with:

UPDATE `pages` p SET p.content = replace(p.content,'â€Â','');
UPDATE `pages` p SET p.content = replace(p.content,'â€Å\“','');
UPDATE `pages` p SET p.content = replace(p.content,'¢â‚','');
...etc

Conclusion

There isn’t anything earth shattering here.  These are simple queries, but they illustrate the inherent abilities of the technology employed with our client’s application.  These inherent abilities, if known, have the potential to save dozens of hours.  In the case of the above example, I truly believe we will likely save our client at least 30 man hours.

Mobify empowers marketers and developers to create amazing mobile web experiences. Tap to learn more

Mobify