[Tutorial] Basic SQL Queries
Posted: Tue Nov 17, 2009 11:04 pm
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.

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:
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.
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:

For the next step we want to get all the people that live in Stockholm
We simply specify that we only want the rows where the column "hometown" has the value "stockholm". This would return the following:

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.
That would work, but not this:
And finally we want to select using several arguments
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
This would return the people that both live in "Stockholm" and has the lastname "Gustavsson".
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.

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;Now to something more advanced, we now only want to get the
first name and hometown.
Code: Select all
SELECT Firstname, Hometown FROM people;load on the SQL server. This would return:

For the next step we want to get all the people that live in Stockholm
Code: Select all
SELECT * FROM people WHERE Hometown = 'Stockholm';
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;Code: Select all
SELECT * FROM people WHERE Hometown = Stockholm;Code: Select all
SELECT * FROM people WHERE Hometown = "Malmö" OR Lastname = "Svensson";You can also use the AND to demand that both are true
Code: Select all
SELECT * FROM people WHERE Hometown = "Stockholm" AND Lastname = "Gustavsson";