• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[Tutorial] How to make basic SQL queries!

Newbie Spellweaver
Joined
Apr 12, 2007
Messages
29
Reaction score
1
First, you should read it all carefully before you start asking questions!
I will only show you how to insert new rows, delete rows, and to get/view rows.
Assuming we are going to use the command promt (CMD) to do this we have to connect and select database first. Notice that I use colored text to refer to the specific parts in the example.

Remember that this is general SQL syntax. Meaning it can be used with many other programs and databases. (You can use this with phpMyAdmin, Mysql GUI tools (query browser), LuaSQL and so on.)

Figure out where mysql.exe is located and go to that directory. It may be that you don't need to do it if you installed it yourself and selected "add to windows PATH" (like me).
Open CMD and write:
Code:
C:\>[B]mysql -u root -p[/B]
Enter password: [B]*******[/B]
mysql>[B]SHOW DATABASES[/B];
+--------------------+
| Database           |
+--------------------+
| information_schema |
| forgotten          |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)
mysql>[B]USE forgotten[/B]
Database changed
mysql>[B]SHOW TABLES;[/B]
+---------------------+
| Tables_in_forgotten |
+---------------------+
| accounts            |
| bans                |
| groups              |
| guild_invites       |
| guild_ranks         |
| guilds              |
| house_lists         |
| houses              |
| player_deaths       |
| player_depotitems   |
| player_items        |
| player_skills       |
| player_spells       |
| player_storage      |
| player_viplist      |
| players             |
| players_online      |
| tile_items          |
| tiles               |
+---------------------+
19 rows in set (0.00 sec)
IMPORTANT: All QUERIES needs an ; to mark the end of the query. (Notice that "USE forgotten" does not need it (=not a query), but "SHOW tables;" needs it.)

We can start off with how to view/get rows:

It is quite easy, logical and simple to understand:
You start of with SELECT, notice, it's easier to read the query if you write all the commands with CAPITAL letters and everything else with normal letters, but not necessary.

After SELECT you write which fields you want to get. If the table got many fields, and your not interested in all of them you simply write the name of the fields you want. If you want to get all fields, just write *

Next you write which table to get it FROM. In the example I'm using the players table.

This is really all you need, but it is with the command WHERE you can do a lot of things.
Lets say we want to get everyone above level 30, name starting with B, group_id like 1 and ordered by last login? Notice: if we are searching for text we need to enclose it in ' and we need to use LIKE to compare. % is wildcard.
To separate the different conditions we use AND or OR depending on how we want it.

Then we use ORDER BY to order it by the field we want. And since we want the one who logged on last to be first we use DESC after the field name, which is short for descending. (default is ascending).

At last we can use LIMIT to restraint the number of results we want.

Also, around every table name and field name it is smart to use ` so that like the field "time" is understood correctly by mysql.

Example:
Code:
SELECT [COLOR=Sienna]`id`, `name`, `level`[/COLOR] [COLOR=DarkOliveGreen]FROM `players`[/COLOR] [COLOR=Navy][COLOR=Purple]WHERE `level` > 30 AND `name` LIKE 'B%' AND `group_id` = 1[/COLOR] [COLOR=DarkOrange]ORDER BY `lastlogin`[/COLOR][/COLOR][COLOR=DarkOrange] DESC[/COLOR] [COLOR=Red]LIMIT 10;[/COLOR]
This was a bit "complex" example, but it can be as simple as this too:
Code:
mysql>[B]SELECT * FROM `groups`;[/B]
+----+--------------+--------------+--------+---------------+------------+
| id | name         | flags        | access | maxdepotitems | maxviplist |
+----+--------------+--------------+--------+---------------+------------+
|  1 | player       |            0 |      0 |             0 |          0 |
|  2 | a gamemaster | 137438953471 |      1 |             0 |          0 |
|  3 | a god        | 267898560504 |      1 |             0 |          0 |
+----+--------------+--------------+--------+---------------+------------+
3 rows in set (0.02 sec)
Now lets move onto deleting.

Its quite similar to SELECT, but doesn't print any results, only how many rows that was deleted.

We can use almost the exact same query, just changing some words.
Here, LIMIT can be your best friend, as it is NOT POSSIBLE to undelete anything!

Lets say we want to delete the 150 oldest players below level 30.

The WHERE part is the same as above, so we'll use what we've already learned.

And to get the 100 oldest players, we need to ORDER BY `lastlogin` and LIMIT 100
Since ORDER BY as I said, default orders ascending, we don't need to add anything more.
With LIMIT 100 we will delete the 100 first rows (and no more) matching the conditions set in WHERE.
Code:
DELETE FROM `players` WHERE `level` < 30 AND `group_id` = 1 ORDER BY `lastlogin` LIMIT 100;
Now maybe the "hardest" part. Inserting new rows.

It is pretty straight forward here too, but a bit more to remember.
Lets say we want to insert a new group.

We start of with INSERT INTO
 
Initiate Mage
Joined
Nov 18, 2006
Messages
1
Reaction score
0
nice, haven't read it yet but i have used the query to clean up my server :) ty
 
Back
Top