Presentation is loading. Please wait.

Presentation is loading. Please wait.

Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Selecting Data.

Similar presentations


Presentation on theme: "Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Selecting Data."— Presentation transcript:

1 Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Selecting Data

2 Open Source Server Side Scripting 2 ECA 236 sitename  the users table contains the following columns users user_idMEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY first_nameVARCHAR(15) NOT NULL last_nameVARCHAR(30) NOT NULL emailVARCHAR(40) passwordCHAR(16) NOT NULL registration_dateDATETIME NOT NULL

3 Open Source Server Side Scripting 3 ECA 236 selecting records  SELECT  the most frequently used of all SQL statements  SELECT returns rows of information based upon a set of selection criteria  the simplest SELECT statement is which returns all the records in the table  the * means ALL SELECT * FROM users;

4 Open Source Server Side Scripting 4 ECA 236 selecting records cont …  specify which columns to return, each column separated by a comma  advantages of specifying columns  performance  order  functions SELECT user_id, first_name, last_name FROM users;

5 Open Source Server Side Scripting 5 ECA 236 conditionals  WHERE  used with a variety of operators to retrieve specific records  equality operator  AND operator SELECT email FROM users WHERE last_name = ‘Lennon’; SELECT email FROM users WHERE last_name = ‘Lennon’ AND first_name = ‘John’;

6 Open Source Server Side Scripting 6 ECA 236 common operators OperatorExplanation =equals <less than >greater than < =less than or equal to > =greater than or equal to ! =not equal to IS NOT NULLhas a value IS NULLdoes not have a value BETWEENwithin a range NOT BETWEENoutside a range INis included in set of listed values NOT INis not included in set of listed values OR ( also || )where one of two conditionals is TRUE AND ( also && )where both conditionals are TRUE NOT ( also ! )where the condition is not TRUE

7 Open Source Server Side Scripting 7 ECA 236 WHERE examples  Select the records for every user who was registered during a range of dates  To SELECT a particular day, set the dates greater than midnight on one day, and less than midnight on the following day SELECT * FROM users WHERE ( registration_date > ‘2003-10-31 00:00:00’ ) AND ( registration_date < ‘2003-11-09 00:00:00’ ); SELECT * FROM users WHERE ( registration_date > ‘2003-11-08 00:00:00 ‘) AND ( registration_date < ‘2003-11-09 00:00:00’ );

8 Open Source Server Side Scripting 8 ECA 236 WHERE examples cont …  Since registration_date is a DATETIME data type, we use the time. If it was simply a DATE data type, we could test to see if the value is equal to a specific day SELECT * FROM users WHERE ( registration_date = ‘2003-11-08’ );

9 Open Source Server Side Scripting 9 ECA 236 WHERE examples cont …  Select the first name of all users whose last name is Starr  Select everything from every record in users that does not have an email address  NULL means there is no value. If a record has an empty string, it is not NULL SELECT first_name FROM users WHERE last_name = ‘Starr’; SELECT * FROM users WHERE email IS NULL; SELECT * FROM users WHERE email = ‘ ’;

10 Open Source Server Side Scripting 10 ECA 236 WHERE examples cont …  Select the record where the password is ‘legsDiamond’ - remember, passwords are case sensitive  Select all the records from users where first_name is in a list of provided values SELECT * FROM users WHERE password = PASSWORD( ‘legsDiamond’ ); SELECT * FROM users WHERE first_name IN ( ‘Leland’, ‘Laura’, ‘Josie’, ‘Bob’ );

11 Open Source Server Side Scripting 11 ECA 236 strings  using numbers, dates, and NULL in a conditional is fairly straightforward  to check for string equality use the = operator  to check for a close, but not equal match, other operators and symbols are available  EG, to select all last names beginning with “Smith” we need a more flexible operator

12 Open Source Server Side Scripting 12 ECA 236 LIKE NOT LIKE  used primarily with strings  use two wildcard characters  underscore _  matches any single character  can be used in combination with itself >LIKE ‘_ _ ‘ will match any two letter combination  percent sign  matches zero or more characters  case insensitive

13 Open Source Server Side Scripting 13 ECA 236 LIKE NOT LIKE cont …  match any user whose last name begins with ‘Smith’  will match ‘Smith’, ‘Smithberger’, ‘Smithsonian’, ‘smithy’, but not ‘Klingensmith’  on the other hand  will match ‘Klingensmith’ SELECT * FROM users WHERE last_name LIKE ‘Smith%’; SELECT * FROM users WHERE last_name LIKE ‘%Smith’;

14 Open Source Server Side Scripting 14 ECA 236 LIKE NOT LIKE cont …  Select the first and last name of every user whose email address is not from aol.com  To use a literal underscore or percent sign in a query, escape them. SELECT first_name, last_name FROM users WHERE email NOT LIKE ‘%@aol.com’; SELECT first_name FROM users WHERE email LIKE ‘sam\_%’;

15 Open Source Server Side Scripting 15 ECA 236 ORDER BY  used to sort query results  if you do not specify ORDER BY, query will most likely be returned by the primary key in ascending order  you can use ORDER BY to sort by any column SELECT last_name FROM users ORDER BY registration_date;

16 Open Source Server Side Scripting 16 ECA 236 ORDER BY cont …  default order when using ORDER BY is ascending ( designated as ASC )  number increase from small to large  dates go from older to most recent  string go from A to Z  to reverse the order specify DESC SELECT first_name, last_name FROM users ORDER BY last_name DESC;

17 Open Source Server Side Scripting 17 ECA 236 ORDER BY cont …  you can ORDER BY multiple columns  return the first and last names ordered by registration date, then last name  if a sorted column contains NULL values, these will appear before columns containing values SELECT first_name, last_name FROM users ORDER BY registration_date, last_name;

18 Open Source Server Side Scripting 18 ECA 236 ORDER BY cont …  you can use ORDER BY along with WHERE to retrieve and order specific records  Return the entire record where any registration date is after November 10, but ORDER BY the user’s last name SELECT * FROM users WHERE registration_date > ‘2003-11-01 00:00:00’ ORDER BY last_name;

19 Open Source Server Side Scripting 19 ECA 236 LIMIT  limits the number of records returned  not part of SQL standard  not available on all databases  LIMIT the number of records returned to 3  return 5 records beginning with the 11 th  begins at index 0 SELECT * FROM users ORDER BY last_name LIMIT 3; SELECT * FROM users ORDER BY last_name LIMIT 10, 5;

20 Open Source Server Side Scripting 20 ECA 236 LIMIT cont …  Select the third person who was entered into the table  the LIMIT n1, n2 clause is great for returning multiple pages of query results where you show the first 10 results, then the next ten, etc. SELECT first_name, last_name FROM users ORDER BY registration_date LIMIT 2, 1;

21 Open Source Server Side Scripting 21 ECA 236 DELETE  DELETE eliminates a record – it does not delete a table or database  once you use DELETE to delete a record, there is no way to retrieve  back up your database before running DELETE  use a WHERE statement with DELETE  otherwise you will delete all data in the table

22 Open Source Server Side Scripting 22 ECA 236 DELETE cont …  will delete everything in the table while retaining its structure  will delete only the specified record  to avoid major errors, log into the mysql monitor with the – – i–am–a–dummy parameter  MySQL will then not allow you to run UPDATE or DELETE without a WHERE clause DELETE FROM users; DELETE FROM users WHERE user_id = 33;

23 Open Source Server Side Scripting 23 ECA 236 DROP  to drop a table  to drop a database DROP TABLE table_name; DROP DATABASE database_name;


Download ppt "Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Selecting Data."

Similar presentations


Ads by Google