Page 1 of 1

[Tutorial] Basic SQL Queries

Posted: Tue Nov 17, 2009 11:04 pm
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".

Re: [Tutorial] Basic SQL Queries

Posted: Tue Nov 17, 2009 11:57 pm
by hallsofvallhalla
very sweet, thanks for this tutorial.

Re: [Tutorial] Basic SQL Queries

Posted: Wed Nov 18, 2009 3:58 am
by SpiritWebb
Very nice...thanks!

Re: [Tutorial] Basic SQL Queries

Posted: Wed Nov 18, 2009 9:53 am
by Falken
Will be coming some more, and it will get more and more advanced, this was just the very foundation on learning more :)