MySQL Programming

MySQL is a fantastic platform to learn database programming, and is also used by lots of very large and fast websites. If you want to get stuck into dynamic web sites, then you could do far worse than the highly popular PHP/MySQL combination that leverages the simplicity and power of PHP with the, well, simplicity and power of MySQL!

How to update fields using grouped data from another table

29 Jan 2011 | Comment
For optimisation purposes you have decided to create a new field in a table, and that field needs to contain a computed value of rows from another table.

Say that your first table will contain the maximum date from the second table, and that there is a one-to-many relationship: field t1id in the second table refers to field id in the first table. Here's the SQL to update all max_date fields in the first table:

UPDATE t1 JOIN ( 
 SELECT t2.t1id, MAX(t2.thedate) maxdate FROM  t2 GROUP BY t2.t1id
) AS agg ON t1.id=agg.t1id
SET t1.max_date=agg.maxdate

I hope that this proves useful; let me know if it does!

Disclaimer: I won't be help responsible for any loss of data or time you suffer as a result of following my advice!


Comments

It's quiet in here...Add your comment

Simple Programming Exercises

Simple Programming Exercises (more)

Technology

Technology The modern world is supported and influenced by technology in a wide variety of ways. Almost nobody in the developed nations can claim to be free of technology, and in fact most homes are stuffed with highly advanced gadgets and ... (more)

Computer Programming

Computer and Web Programming (more)

Web Programming

Web Programming This section is all about web development, helping programmers to develop the best sites possible. The key elements of successful web development: Performance - Interface - Maintainability (more)

UK PHP Conference 2012: a question of scale

I was fortunate enough to attend the PHP UK Conference 2012, on Friday 24 February. The theme of the conference was about how well PHP scales; watch the two keynote speeches by Rasmus Lerdorf (PHP founder) and Hugh Williams (eBay's Engineering ... (more)

Rasmus Lerdorf: Founder of PHP

Greenland has its very own computing influencer! Way back in 1995, Rasmus Lerdorf got PHP (PHP Hypertext Preprocessor) off the ground and thus paved the way for a blindingly simple way to do web programming. PHP placed the emphasis of website ... (more)

Geo-targetting your website

Does your company have a local presence in many different countries, or do you run a site where it makes sense to keep visitors from different countries away from each other? There are a number of follow-on questions here. Does it matter ... (more)

Get a CentOS Server Web-Ready

Here are the command-line steps needed to get a bare Rackspace Cloud CentOS 5.5 image ready for full LAMP (Apache-MySQL-PHP) hosting: (Note - replace mysite.com with the domain name of your own website) yum install rsync yum install mysql ... (more)

The Power of Cloud Computing

Cloud computing promises much for the budget-conscious web publisher. It should allow - within reason - a flexibility of resource availability so that bursts in traffic are absorbed by spare computing utility. In practice, the publisher still ... (more)

Windows 7: Adding a context menu item for image resizing

Install imagemagick Create a destination directory for the images Create a batch file (eg, C:\webimage.bat) with this line in it: convert %~nx1 -resize 900x700 C:\web\%~nx1 Download and install ContextEdit: http://www.softpedia.com/progDownload ... (more)

SANS Institute: Top 25 Programming Mistakes

The SANS Institute has published a list of the 25 most dangerous programming errors - and how to fix them. See CWE/SANS TOP 25 Most Dangerous Programming Errors. Two of the errors accounted for 1.5 million breaches during 2008, so it is important ... (more)

How to Use the Twitter API

It's really easy to integrate Twitter notifications into your own web app. For the source code have a look at Programming with the Twitter API. (more)

Daylight Saving Changes

The United States Energy Policy Act of 2005 established new rules for Daylight Savings Time (DST) in the US. These new rules change the start and end dates for DST beginning 2007. This year, DST will start on the second Sunday of March and ... (more)

Google Search

Code from this page: Build a Site Search with the Google Search API (more)

Recent Updates:

19 May : Heating Oil Thefts - Stay Alert!
19 May : Pro Blaze Overshoes pass the test
15 May : Get a CentOS Server Web-Ready
13 May : Keeping your iPhone secure
06 May : Nero, Claudius and a rather dead mouse
19 May : Domestic Heating Oil Prices: Know the Market!
19 May : Sports Gear: Under Test
15 May : Rackspace Cloud Hosting
13 May : iPhone 4
06 May : Cats
19 Mar : Time Trial Results
25 Nov : Piano for the Amateur
14 Nov : Measure
02 Nov : Book Reviews
12 Oct : Simple Programming Exercises
Web Development Survey!
Which web technologies would you like to see the back of, and why?

 

Popular Searches: