SQL Programming summary

1. Find

* The FROM Websites the SELECT;
the SELECT the DISTINCT Country the FROM Websites; // only difference value selecting
the SELECT * the FROM Websites the WHERE Country = 'the CN';
= equal to
<> Not equal. NOTE:! In some versions of SQL, the operator may be written as =
> greater than
<less than
> = Greater than or equal to
<= less than or equal
BETWEEN within a certain range
LIKE search a pattern
IN designates possible for a plurality of columns value

AND OR
SELECT * FROM Websites ORDER BY alexa DESC;
SELECT * FROM Websites ORDER BY country,alexa;

2, insert

INSERT INTO Websites (name, url, alexa, country)
VALUES ('百度','https://www.baidu.com/','4','CN');


3, update
the UPDATE Websites
the SET ALEXA = '5000', Country = "USA"
the WHERE name = 'novice Tutorial';


4, remove
the DELETE the FROM Websites
the WHERE name = 'Baidu' AND country = 'CN';


5, lookup attributes
the SELECT * 2 the OFFSET the FROM Websites the LIMIT 2;
the SELECT * the FROM Websites the WHERE name the LIKE '%% OO';
% replacement of one or more characters
_ substitute only one character
[the charlist] Any single character string character
[^ the charlist] or [the charlist] is not any single character column character!
the SELECT * the WHERE name REGEXP, the FROM Websites '^ [the AH]';

SELECT * FROM Websites WHERE name IN ('Google','菜鸟教程');
SELECT * FROM Websites WHERE (alexa BETWEEN 1 AND 20) AND country NOT IN ('USA', 'IND');
SELECT name AS n, country AS c FROM Websites;
SELECT w.name, w.url, a.count, a.date FROM Websites AS w, access_log AS a WHERE a.site_id=w.id and w.name="菜鸟教程";


6, connected
INNER JOIN: If the table has at least one match line is returned
LEFT JOIN: even if there is no match in the right table, the table also return all rows from the left
RIGHT JOIN: even if there is no match in the left table, table returns from right All rows
FULL JOIN: As long as there is a match where a table, return line

UNION operation result set operator for combining two or more of the SELECT statement.
The FROM Country Websites the SELECT
UNION
the SELECT Country the FROM Apps
the ORDER BY Country;

SELECT country FROM Websites
UNION ALL
SELECT country FROM apps
ORDER BY country;

7, SQL functions
AVG () - returns the average of
COUNT () - Returns the number of rows COUNT the SELECT (the DISTINCT SITE_ID) the FROM access_log the nums the AS;
FIRST () - returns the first record of the value
LAST () - returns the last recorded value
MAX () - returns the maximum value of
MIN () - returns the minimum value
sUM () - returns the sum

UCASE () - a field will be converted to uppercase
LCASE () - a field will be converted to lowercase
MID () - extract characters from a text field, MySql use
SubString (field, 1, end) - From a text extracting a character field
LEN () - returns the length of a text field
ROUND () - rounding specified number of decimal places of a numeric field
NOW () - returns the current system date and time
fORMAT () - a field formatted display mode

GROUP BY
SELECT site_id, SUM(access_log.count) AS nums
FROM access_log GROUP BY site_id;

Websites.name the SELECT, COUNT (access_log.aid) AS nums access_log the FROM
LEFT JOIN Websites
ON access_log.site_id = Websites.id
the GROUP BY Websites.name;

the HAVING (the HAVING clause allows us to filter data packets in each group)
the SELECT Websites .name, the SUM (access_log.count) the FROM Websites the AS the nums
the INNER the JOIN access_log
the ON Websites.id = access_log.site_id
the WHERE Websites.alexa <200 is
the GROUP BY Websites.name
the HAVING the SUM (access_log.count)> 200 is;

 

Guess you like

Origin www.cnblogs.com/share-ideas/p/11260618.html