[Tutorial] Basic SQL Queries

Post all your tuts or request for tuts here.
Post Reply
Falken
Posts: 438
Joined: Fri May 08, 2009 8:03 pm

[Tutorial] Basic SQL Queries

Post by Falken »

Ok, so to make a good php game or similair it is important to know how to use the SQL databases. So therefore I thought I would make a SQL tutorial. And we will be using SQL queries instead of php admins. SQL Queries are very powerful and can reduce php code alot.

This will be the most basic parts of SQL queries. Below is the database we will use, so go ahead and add this to your database.

I called the database "people" and it is filled with some made up swedish people.

Image

Making a choice
In this first part we will go through choosing the right rows and columns, very basic, but still so important, read it carefully, there is a in many cases some new small details you can learn :)

We will start by the most basic, returning all posts:

Code: Select all

SELECT * FROM people;
The * means "all columns". That would return all rows.

Now to something more advanced, we now only want to get the
first name and hometown.

Code: Select all

SELECT Firstname, Hometown FROM people;
So we do as before, but instead change the * to which columns we want to include. This is a good way to reduce
load on the SQL server. This would return:

Image

For the next step we want to get all the people that live in Stockholm

Code: Select all

SELECT * FROM people WHERE Hometown = 'Stockholm';
We simply specify that we only want the rows where the column "hometown" has the value "stockholm". This would return the following:

Image

If you want to get rows with a specific ID, or other number, you would not need the ' around it, that is just
for dates, strings, and so on.

Code: Select all

SELECT * FROM people WHERE ID = 1;
That would work, but not this:

Code: Select all

SELECT * FROM people WHERE Hometown = Stockholm;
And finally we want to select using several arguments

Code: Select all

SELECT * FROM people WHERE Hometown = "Malmö" OR Lastname = "Svensson";
This would return all people that has the Lastname "Svensson" or that live in "Malmö".

You can also use the AND to demand that both are true

Code: Select all

SELECT * FROM people WHERE Hometown = "Stockholm" AND Lastname = "Gustavsson";
This would return the people that both live in "Stockholm" and has the lastname "Gustavsson".
--- Game Database ---
For all your game information needs!
Visit us at: http://www.gamedatabase.eu today!
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: [Tutorial] Basic SQL Queries

Post by hallsofvallhalla »

very sweet, thanks for this tutorial.
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: [Tutorial] Basic SQL Queries

Post by SpiritWebb »

Very nice...thanks!
Image

Image
Falken
Posts: 438
Joined: Fri May 08, 2009 8:03 pm

Re: [Tutorial] Basic SQL Queries

Post by Falken »

Will be coming some more, and it will get more and more advanced, this was just the very foundation on learning more :)
--- Game Database ---
For all your game information needs!
Visit us at: http://www.gamedatabase.eu today!
Post Reply

Return to “Tutorials”