MySQL DAY2 ================= create database ...; show databases; use test; create table orders( o_id int not null, o_name varchar(50), date datetime, c_id int, amount decimal(10,2) ); show tables; insert into orders values (100,'computer','2011-11-1 12:34:56',1,8800), (101,'iphone','2011-10-10 15:20:45',3,6600), (102,'microphone','2017-09-13 09:34:56',4,450), (103,'ipad','2014-02-27 18:24:17',4,9200), (104,'iwatch','2017-09-13 10:34:22',2,5700), (105,'mouse','2012-06-23 09:44:33',5,200), (106,'pen','2013-03-31 21:34:23',6,300), (107, 'iPhone', '2016-04-23 07:. 19: 22 is', 7,7800), (108, 'iPad', '2016-02-12 20 is: 20 is: 20 is', 5,12000); Orders from * SELECT; 1.update Update table set value 1 = 1 column name, column name value 2 = 2, ..., n = the value of the column name n- WHERE condition; example: Update the Customers set the salary = 3500 WHERE name = 'Kate'; Note: update language if you do not write where conditional statements, will change the value in the table all the rows. Exercise: the customers table, age greater than or equal 25 years of age, and the address is Beijing or Shanghai, so the people of wage increases by 10% Tip: Update, and, (), or, the WHERE the SELECT * from customers the WHERE Age> = and 25 (address = 'Beijing' or address = 'of Shanghai'); Update the Customers SET the salary the salary = 1.1 * WHERE Age> = 25 and (address = 'Beijing' or address = 'of Shanghai'); select salary * 2 from customers; Note: Mysql5.7 not recognize%, expressed as a decimal. 2.delete delete from table where conditions; example: delete from the Customers where the above mentioned id = 8; Note: delete statement if there is no where conditions will delete all the data in the table. delete from table name; <=> truncate table table name; 3.count (*) indicates how many records in the current table, a total of. COUNT SELECT (*) from the Customers; SELECT COUNT (*) Total from the Customers; SELECT COUNT (*) AS Total from the Customers; 4.like wildcard%: represents 0, 1 or more characters. Wildcards _: on behalf of a number or character. Note: 100.00 float or matching ___.__% ___. SELECT * from table where column name like 'XXXX%'; SELECT * from table where column name like '_X_X'; Example: identify customers table , name that begins with the customer's name and age B select name, age from customers the WHERE name like 'B%'; the SELECT name, the customers Age from the WHERE name like 'b%'; Find salary is four digits, and is 7,000 yuan customer information the SELECT * from the Customers the WHERE salary like '7 ___.__'; the SELECT * from the Customers the WHERE salary like '. 7 ___%'; to find the fourth-last address character is z, and the third character is the inverse of h, such as the customer's name and address SELECT name, address from the customers WHERE address like '% zh__'; SELECT * from the customers WHERE address like '%% ZH'; 5.limit limit select * from table limit n; where, n refers to the n rows of the table before return. Examples: SELECT * from the Customers limit. 3; <=> SELECT * from limit the Customers 0,3; SELECT * from table limit m, n; starts from m + 1 records, returns the n. note: Index table in the first row is 1 limit statements on the sql language last forever. Exercise: Returns the customers, the first data into two 5 select * from customers limit 1,4; return customers table, salary greater than 4,000 yuan, the first three meet records such conditions Tip: limit statement is always placed at the end sql language select ... where ... limit ... ; select * from the Customers where the salary> 4000 limit. 3; 6. The column name sort order by ASC / desc the ASC ASC DESC descending select from table column names where conditions order by column name ASC; example: select * from the Customers order by ID; the SELECT * from the Customers the Order by the above mentioned id desc; the SELECT name, Age from the Customers the Order by Age desc; the SELECT name from the Customers the Order by Age asc; the SELECT name from the Customers the Order by Age; Note: order by statements behind the appearance of the column names, you can not select statement. insert into customers values (9, ' Lilei', 25, 'beijing', 12000.00); the customers in the city for the Beijing customers, in ascending order older the SELECT * from customers the WHERE address = 'beijing' the Order by Age asc; the customers, the cities of Beijing customers, sort in ascending age, the age of the same arranged in descending order of salary. * customers from the SELECT the WHERE address = 'beijing' the Order by Age asc, desc salary; the customers, the city of Beijing for the customer, in ascending order according to age, the age of the same salary in descending order, only to return a result in the first Records. * the Customers from the SELECT the WHERE address = 'beijing' the Order by asc Age, salary desc limit 1; Questions: The following conditions are satisfied customers of wage increases% 10: address the fourth-last character is z, and the third last character It is h. Tip: update, like, a wildcard Note: In Comparative mysql and assignment operator is =. update customers salary = salary * 1.1 the SET the WHERE address like '% zh__'; wages are customer-digit names and salaries, wages printed in descending order of the SELECT name, salary from the Customers the WHERE salary like '_____.__' the Order by salary DESC; sELECT name, the salary from customers WHERE like the salary '_____%.' Order by the salary desc; selected data is arranged in the N position is selected customers table, pay ranked fifth customer information sELECT * from customers Order desc salary by limit 4,1; selected customers table, salary information came in the penultimate one of the customer's Tip: by the Order, limit select * from customers the Order by salary limit 1; 7. randomly selected data in the table select * from table Order by RAND () limit n-; Example: the orders table, three randomly selected data Orders from * SELECT Order by RAND () limit. 3; 8.group by packet, HAVING statement, aggregation function. Often together with the aggregate function count () Total sum SUM () AVG () average min () Min max () maximum select a column name, ..., column names n, aggregate functions (expression) from table where conditions group by the name of the column 1, ..., column name n- Order by ...; example: SELECT address, max (the salary) from the Customers group by address; demo error: SELECT name, address, max (the salary) the Customers from Group by address; Note: 1.group where statement by statement must be placed after the prior order by the statement columns after 2.group by, must appear in the select statement, aggregation function or expression. select statement in the column, if not now aggregate function expression, you must appear in group by statement Exercise: Tip: count (*) Lists all the city's average wage, according to the average wage ascending sort Tip: AVG (), Group by, the Order by the SELECT address, AVG (salary) from the Customers the WHERE address = 'beijing'! Group address by the Order by AVG ( the salary); SELECT address, AVG (the salary) from the Customers group by address Order by AVG (the salary); With aggregation function, to use the general group by statement. SELECT address, AVG (the salary) from the Customers Group by address HAVING address = 'Beijing'! Order by AVG (the salary) limit 2; SELECT address, AVG (the salary) from the Customers WHERE Not (address = 'Beijing') Group by address Order AVG by (salary) limit 2; lists various cities of the total number of customers select address, count (*) from customers group by address; Questions: lists the average urban wage greater than 4000 cities information having statement: packet generated by the screening group by example: the SELECT address, AVG (salary) from the Customers group by address having AVG (salary)> 4000; demo error: where aggregate functions can not appear statement SELECT address, AVG (the salary) from the Customers WHERE AVG (the salary)> 4000; [Note] 1.having statement usually in combination with group by, for filtering the group by statement a collection of records returned. There is 2.having statements can not make up for the deficiencies where statements used in conjunction with aggregate functions. select a column name, ..., column names n, aggregate functions (expression) from table where conditions group by the name of the column 1, ..., column name n- HAVING condition 1, ..., n-condition; exercises : lists the various cities, older than 25 customers inside the highest revenue year-old, the SELECT address, max (salary) from the customers and the highest income of not less than 3,000 yuan, these records are arranged in ascending highest income Tip: max (), the WHERE, Group by, the HAVING, the Order by ... the WHERE Age> 25 Group address by the HAVING max (salary)> = 3000 the Order by max (salary); the average wage of non-listed cities of Beijing , according to the average wage in ascending order, and only the top two listed city information [a] method for the SELECT address, AVG (salary) from the Customers Group address by the HAVING address! = 'Beijing' the Order by AVG (salary) limit 2; [method II] SELECT address, AVG (the salary) from the Customers ! WHERE address = 'Beijing' group by address Order by AVG (the salary) limit 2; [Note] group by statement without, error. [Method] three SELECT address, AVG (the salary) from the Customers WHERE Not (address = 'Beijing') Group address by the Order by AVG (salary) limit 2; [Method IV, sub-queries, speak tomorrow] address select, AVG (the salary) from the Customers WHERE address in ( 'Beijing', 'of Shanghai') Group by address; 9.distinct removing duplicates, extract the unique record, together with a select statement select distinct column name table 1 from where conditions; Example: address class view of the customers table: SELECT DISTINCT address from customers Order by address; see the address of the category number of customers table: SELECT COUNT (DISTINCT address) from customers; SELECT COUNT (DISTINCT address) from cityNum customers; the SELECT COUNT (DISTINCT address) AS cityNum from customers; view customers in the following table for each address class, there are several customers: the SELECT address, COUNT (*) from customers Group by address; the SELECT address, count (address) from customers Group by address; 10.as to the table or column from a temporary alias select the column name from the table name as the new name of the table; select a new column names as column names from table name; example: select as c_id ID from customers; select c_id ID from customers; Exercise: selecting the maximum salary customers table, and rename MAX_SALARY select max (the salary) as MAX_SALARY the Customers from; SELECT max (the salary the salary-0.1 *) from the Customers AS MAX_SALARY; 11. The joint multi-table queries. They are connected using the same column value contained in two tables together. select a column name, column name 2, ..., n from the column names in the first table, the second table where the first column name table x = a second table column names y;.. Examples: select ID, name, salary, o_name, amount from the customers, the Orders the WHERE Customers.ID = orders.c_id; exercise: Print out the name customer spending limit amount exceeds its wage salary of personnel, salary, amount Tip: the SELECT ... from .. . the WHERE ... and ... the SELECT name, salary, AMOUNT from the Customers, the Orders WHERE Customers.ID and orders.c_id AMOUNT => the salary; 12.union: Comprehensive plurality of select statement, and returns no duplicate rows select column names 1, .., n from the column names in Table 1 WHERE conditions Union sELECT column name 1, .., n 2 from the column name table where conditions; example: select the customers table, younger than 25 years old and 27 years of age is greater than the names and ages method 1: or sELECT name, from customers Age where age <25 or age> 27 ; method 2: Union select name, Age from the Customers WHERE Age <25 Union select name, Age from the Customers WHERE Age> 27; [Note] 1. each of the select statement must select the same number of column. 2. The data type of the column to be consistent. 3. The order of the columns to be consistent. Exercise: Print out the customers table, all the customer's wages, and in the last line, print out their total wages. Tip: Union, SUM () the SELECT salary from customers Union SELECT SUM (the salary) from the Customers; structural change table 13.alter add a column alter table table add Column Name Data Type; example: alter table add the Customers Gender char (10); delete column alter table column name table drop column; example: ALTER the Customers table drop column Gender; add columns, and set the default values: ALTER tABLE table name add default default name data type column; example: ALTER add the Customers table Gender char (10) default 'F'; exercise: 1. Fill the orders table, adding a company called company, and set the default value of apple Apple Tip: ALTER table, default ALTER orders the Add company table VARCHAR (20 is) default 'apple'; 2 .orders table, the o_name company orders not to begin with i unified modify for other others tips: Update, not, like,% Update the orders company = SET 'Others' WHERE o_name Not like 'I%'; 3. the order record company apple is selected, sorted according to descending amount Tip: Group by, Order by SELECT * from Orders WHERE company = 'apple' amount by desc Order; 4. orders recorded in accordance with the category of the packets company, the company prints, the maximum value and the amount of each group, the lowest value, average and total tips: group by, min (), max (), AVG (), SUM () SELECT company, max (AMOUNT), min (AMOUNT), AVG (AMOUNT), SUM (AMOUNT) orders from Group by Company; 5. the recording of orders, grouped by company, Print amount of the sum came in the information group first. Tip: Group by, HAVING, by Order, limit SELECT Company, SUM (AMOUNT) Orders from Group Company by Order by SUM (AMOUNT) desc limit. 1;
mysql day2.txt
Guess you like
Origin blog.csdn.net/weixin_42193179/article/details/103859247
Recommended
Ranking