Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
100 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Common questions

How do I use this knowledge on the web?
You visit www.php.net or www.aspin.com and look up some decent tutorials on the language you want to use. Search terms such as "connect to database" would be a good start. Now that you know a little more about SQL the examples should be easy as 1,2,3.

How do I search for news newer than [a date]?
This one gave me a hard time when I was a beginner. The answer for returning all news post OLDER than 2003-01-01 is:

SELECT * from news where newsDate<'#2003-01-01#' ORDER BY newsDate DESC

It's the ## which does the magic. < means "less than" and > means "greater than". Figure it out!

How do I search a field for a given string?
This is good if you want to do a little search function for your forums or news perhaps. Use the LIKE statement as follows:

SELECT * from news WHERE newsBody LIKE "%futurama%"

The % signs are wildcards telling the database that there can be other text both in front of and after the search string. This query will return all news where the body contains the word futurama.

How do I count news items?
This is also a very common question, and a nice feature to add for that little extra on your page. Try the following code to count the number of news posts Albert Sandberg has posted (before it was all deleted by the hero):

SELECT COUNT(*) from news where memberID=1

How do I figure out the average age of my members?
Let's say you have a entity of the table member which is called memberAge, where you store the age of the member visiting your site:

SELECT AVG(memberAge) AS age from member

Here I showcased the AS command too. This way you can name your results. This comes in handy when you select multiple (otherwise) nameless variables. In the following examples, utilizing some other nice functions in SQL, using no naming simply wouldn't be appropriate (it would still work though):

SELECT AVG(memberAge) AS average, MIN(memberAge) AS youngest, 
       MAX(memberAge) AS oldest FROM member

That's it!

Final words

This has been one crazy night for me. My hands hurt from all the typing and I would really appreciate if you told me if you liked the article or have anything to add about it. My email at thec@home.se is always open for your comments and I'd love to hear from you. I hope this article has given you something to build on and that it has been some pleasant reading.

SQL is really a powerful tool to know, and the more you know, the more fun it will be. There are many aspects of SQL which I haven't shown here. Perhaps I'll write another article some time, only time can tell.

Thank you for your time.
Albert "thec" Sandberg
thec@home.se

About the author

You're going to be surprised. Although I love electronics and know a lot of programming in many senses and have been studying programming at the university for a while, I'm a tile layer. That's like a bricklayer only that I just do the tiles part. That is, I do bathrooms, kitchens, floors and balconies and such. Please check my homepage www.thec.org for some pictures and extended information about myself.

During a couple of years in the industry and lots of home programming I mostly program/manage hobby sites and such, as well as doing some C++ code when I lose my mind... sorry, I mean am in the right mood for it!

Other than computers, I love cars and snowboarding, so that's basically my real life activities.




Contents
  Introduction
  Commands
  FAQ

  Printable version
  Discuss this article