【课程学习】SQL-Learning
Mysql tutorial for beginners
Creating the Databases
The LIKE Operator
Get all the custmers who’s patterns start with b/B
1 | SELECT * |
get all the custmers who’s last name start with brush
1 | SELECT * |
don’t care about the first character but the second character is y
1 | SELECT * |
- % : any number of characters
- _ : single character
EXERCISE
- Get the customers whose addresses contain TRAIL or AVENUE
1 | SELECT * |
- Phone numbers end with 9
1 | SELECT * |
The REGEXP Operator
REGEXP: regular expression
REGEXP is similar with LIKE
Get all the customers who’s last name start with field
1 | SELECT * |
Get all the customers who’s last name end with field
1 | SLECT * |
Get all the customers who have words field,mac or rose in their last name
1 | SELECT * |
Get all the customers who have words ge,me or ie in their last name
1 | SELECT * |
- ^ beginning
- $ end
- | logical or
- [abcd]
- [a-f]
EXERCISE
GET the customers whose
- first names are ELKA or AMBUR
1 | SELECT * |
- last names end with EY or ON
1 | SELECT * |
- Last names start with MY or contains SE
1 | SELECT * |
- last name contain B followed by R or U
1 | SELECT * |
The IS NULL Operator
search who does not have a phone
1 | SELECT * |
EXERCISE
get the orders that are not shipped
1 | SEARCH * |
The ORDER BY Clause
In this tutor I’m going to show you how to sort data in your sequel queries
Search customers order by first_name
1 | SELECT * |
or you want to reverse the sort order
1 | SELECT * |
sort by multiple columns, for example
1 | SELECT * |
The LIMIT Caluse
get the first 3 customers
1 | SELECT * |
Get customers form 6-9
1 | SELECT * |
6 is an offset, and 3 means the step
EXERCISE
- Get the top three loyal customers
1 | SELECT * |
The Inner loins
JOIN is equal with INNER JOIN , we don’t have to type it.
you can use JOIN to catch the relation between to tables;
For example, you want to search orders which have the same customer_id in table CUSTOMERS from the table ORDERS.
1 | SELECT * |
we can use alias to simplify the query.
1 | SELECT * |
if we use o as the orders’s alias , we can’t write orders to instead o in the next.
Joining Across Databases
how to combine columns from tables in multiple databases ?
Using database combines with sql_inventory database.
1 | SELECT * |
the query will be different depending on the database
Self Joins
In sql we can join tables with itself.
In database sql_hr we have a table named employees.
Now we need select each employee and their manager.
1 | USE sql_hr; |
Joining Multiple Tables
1 | SELECT |
Exercise
In database sql_invoicing we have this table, payments and these are the payments that each client has made towards either invoice. We also have a table named payment_methods.
Write a query that join the payments with the payment methods tables as well as the clients table.Produce a report that shows the payments, with more details, such as the name of the client and the payment method.
1 | SELECT |
Compound Join Conditions
we have mutiple conditions to join these two tables
e.g.
1 | SELECT * |
Implicit Join Syntax
In Mysql we can use simple query to instead Join condition.
for example
1 | SELECT * |