<<Continued>>
Basic SQL Clauses
This is a subsection to the SQL section of this tutorial. In this section, you will learn basic clauses and operators. Clauses are used to further effect your queries. With them, you can return specific rows, etc. You will recieve an example of each clause within each command I taught you except for the INSERT INTO, DROP TABLE, and TRUNCATE TABLE commands.
WHERE
The WHERE clause can be used to select specific rows from a table where a row within a column is equal to something. Examples:
SELECT * FROM userinfo WHERE Age=13
This would return:
Kurtis Dinelle 13
because we selected all the rows from the userinfo table where each row in the Age column is equal to 13.
UPDATE userinfo SET First_Name='Retarded' WHERE Last_Name='Child'
This would return:
Kurtis Dinelle 13
Bob Doyle 30
Jimmy Wells 17
Retarded Child 6
because we selected the rows where the row in the Last_Name column is equal to Child.
DELETE FROM userinfo WHERE First_Name='Bob'
This would return:
Kurtis Dinelle 13
Jimmy Wells 17
Smarter Child 6
because we delete the row in the userinfo table where every row in the First_Name column is equal to Bob.
ORDER BY
The ORDER BY clause is used to order data selected from a table in a certain way. If you order by a column containing a string, it will be ordered alphabetically. If you order by a column containing an integer, it will be ordered by value. You can even go as far as ordering data in ascending order or descending order by adding either ASC or DESC to the end of the ORDER BY clause (ASC is used by deafault). If you order a string column in ascending, it will be ordered in alphabetic order. If you order a string column is descending, it will be ordered in reverse alphabetic order. Same for integer columns. If you order one by ascending, it will be ordered from least to greatest. If you order it in descending order, it will be ordered from greatest to least. Since ORDER BY is only used with SELECT, you will only recieve examples in SELECT, but you will have 4 examples showing the 4 different ways of ordering.
SELECT * FROM userinfo ORDER BY Age
This would return:
Smarter Child 6
Kurtis Dinelle 13
Jimmy Wells 17
Bob Doyle 30
because we selected all the rows and ordered them by the Age column in ascending order.
SELECT * FROM userinfo ORDER BY Age DESC
This would return:
Bob Doyle 30
Jimmy Wells 17
Kurtis Dinelle 13
Smarter Child 6
because we selected all the rows and ordered them by the Age column in descending order.
SELECT * FROM userinfo ORDER BY First_Name
This would return:
Bob Doyle 30
Jimmy Wells 17
Kurtis Dinelle 13
Smarter Child 6
because we selected all the rows and ordered them by the First_Name column in ascending order.
SELECT * FROM userinfo ORDER BY First_Name DESC
This would return:
Smarter Child 6
Kurtis Dinelle 13
Jimmy Wells 17
Bob Doyle 30
because we selected all the rows and ordered them by the First_Name column in descending order.
LIKE
The LIKE clause is used with the WHERE clause. You can use % signs as wildcards in these "patterns".
SELECT * FROM userinfo WHERE Last_Name LIKE '%ell%'
This would return:
Kurtis Dinelle 13
because we selected the rows where each row in the Last_Name column begins with anything, has ell in it, and ends with anything.
UPDATE userinfo SET First_Name='Dan' WHERE First_Name LIKE 'Jimmy'
This would return:
Kurtis Dinelle 13
Bob Doyle 30
Dan Wells 17
Smarter Child 6
because we updated the rows where the rows in the First_Name column is equal to Jimmy.
DELETE FROM userinfo WHERE Age LIKE '1%'
This would return:
Bob Doyle 30
Smarter Child 6
because we delete all the rows where the rows in the Age column begin with a 1, but can end in anything.
LIMIT
The LIMIT clause is used to return a certain amount of rows from a query. It can be used in two ways, but Im only going to show the first, basic way because I have been writing this tutorial for ages :s
SELECT * FROM userinfo LIMIT 1
This would return:
Kurtis Dinelle 13
because we selected rows from the userinfo table, but restricted it to only select 1 row.
UPDATE userinfo SET First_Name='Dan' LIMIT 2
This would return:
Dan Dinelle 13
Dan Doyle 30
Jimmy Wells 17
Retarded Child 6
because we updated only the first 2 rows.
DELETE FROM userinfo LIMIT 3
This would return:
Smarter Child 6
because we delete the first 3 rows.
Phew! That part was done. Note that you can use any of these clauses together.
--------------
Basic SQL Operators
Welcome to the last learning section of this tutorial. In this section, you will be learning about basic sql operators you can use with the WHERE clause, and two "logical" operators (if you can call them that, I don't know) AND and OR. I won't be giving many examples, just what each of these few operators do.
= - Checks if two values are equal.
!= - Checks is two values are not equal.
> - Checks if value 1 is greater than value 2.
< - Checks if value 1 is less than value 2.
AND - Checks if all of multiple expressions are true.
OR - Checks if one of multiple expressions is true.
Sorry if I missed some ither basic ones, like I said before, Ive been writing this for ages, and can't be arsed to think and/or research.
--------------
Mysql References and Helpful Software
Documentation of mysql functions
w3schools SQL tutorial
phpmyadmin
--------------
Conclusion
I hope you learned something from this massive tutorial that took me ages to write >:(
Since this tutorial is over 2,450 words long, and 14,700 characters long, I don't feel like proof reading it. So please post here any errors or mistakes and enjoy!