In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what are the sql sentences in MySQL, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
The following two tables are explained.
SELECTSELECT "field" FROM "table name"; SELECT Store_Name FROM Store_Info; displays all data records for one or more fields in the table
DISTINCTSELECT DISTINCT "field" FROM "table name"; select distinct store_name from store_info; does not display duplicate data records
WHERESELECT "field" FROM "table name" WHERE "condition"; find the store_name field of the symbol sales=700 condition in the store_info table with conditional query
AND ORSELECT "field" FROM "table name" WHERE "condition 1" select store_name,sales,date from store_info where sales > 1000 or (sales
< 500 and sales >Or INSELECT "field" FROM table name "WHERE" field "BETWEEN" ('value 1' AND 'value 2); select * from store_info where store_name in (' Houston','Los Angeles'); data record showing known values
BETWEENSELECT "field" FROM "table name" WHERE "field" BETWEEN 'value 1' AND 'value 2 select * from store_info where sales between 200 and 500; displays two value ranges
Wildcard character
%; the percent sign indicates zero, one or more characters
_: an underscore represents a single character
All strings that begin with'A', have another character of any value, and end with a'Z'. For example, 'ABZ'' and 'A2Z' both match this pattern, while 'AKKZ' does not (because there are two characters between An and Z, not one character).
ABC%': all strings that start with 'ABC'. For example, both' ABCD' 'and' ABCABC' match this pattern.
'% XYZ': all strings that end in' XYZ'. For example, 'WXYZ' and' ZZXYZ' both match this pattern.
'% AN%': all strings that contain the pattern' AN'. For example, both 'LOS ANGELES'' and 'SAN FRANCISCo'' fit this pattern.
'_ AN%': all strings with the second letter' A 'and the third letter' N'. For example, 'SAN FRANCIScO'' fits this pattern, while 'LoS ANGELES' does not.
LIkeSELECT "field" FROM "table name" WHERE "field" LIKE {schema}; select * from store_info where store_name like'% os%'; matches a pattern to find the data record we want
ORDER BYSELECT "field" FROM "table name" [WHERE "condition"] ORDER BY "field" [ASC,DESC]; select * from store_info order by sales;select * from store_info order by sales desc;#ASC is sorted in ascending order, which is the default sorting method. # DESC is sorted in descending order
Function
Mathematical function
Abs (x) returns the absolute value of x rand () returns a random number from o to 1 (mod (x)) returns the remainder power (x) after dividing x by y Y) returns the round (x) to the power of x returns the integer round (x) nearest to x) the value sqrt (x) returns the square root of x truncate (x) after the y places of x are rounded up Y) returns the number x truncated to y decimal places ceil (x) returns the smallest integer floor (x) that is greater than or equal to x, returns the largest integer greatest (x1 ~ x2...) Returns the largest value in the collection, least (x1 ~ x2...). Return the smallest value aggregate function in the set: avg () returns the average of the specified column count () returns the number of non-null values in the specified column min () returns the minimum value of the specified column max () returns the maximum value of the specified column Sum (x) returns the sum of all the values of the specified column
City form
# count (*) includes the number of rows of all columns. The number of rows whose column value is NULL#count (column name) only includes the column name will not be ignored when counting the result. The line string function trim () whose column value is NULL is ignored and returns the value that removes the specified format concat (XMague y) splices the supplied parameters x and y into a string substr (XMague y) to get the string starting at the Yth position in the string x It works the same as the substring () function substr (xpene y) Z) gets the length of the string length (x) that returns the length of the string x from the y position in the string x Z) replace string z with string yupper (x) in string x turn all letters of string x into uppercase letters lower (x) and all letters of string x into lowercase letters left (x) Y) return the first y characters of the string x right (x) return the last y characters of the string x repeat (x space y) return the string x repeat y times space (x) return x spaces strcmp (x Y) compare x and y The returned value can be-1 reversing the string x with a common function instance: concat
If sql_mode turns on PIPES_AS_CONCAT,' | |'as the concatenation operator of the string rather than the OR operator, it is similar to the concatenation function Concat of the string, which is similar to Oracle and is used in the same way as the Oracle database
Substr
Trim
Region
Replace
Group byselect "field 1", sum ("field 2") from "table name" group by "field 1"; group by has a principle that in all columns after select, columns that do not use aggregate functions must appear after group by
Having
Used to retrieve the recordset returned by the group by statement, usually in conjunction with the group by statement
The existence of the having statement makes up for the deficiency that the where keyword cannot be used in conjunction with the aggregate function. If there is only a function column in the select, then the group by clause is not needed.
Alias select "table alias". Field one "[AS]" field alias "from" table name "[AS]" table alias; field alias, table alias
Subquery select "field 1" from "table" where "field 2" [comparison operator] # external query select "field 1" from "table 2" where "condition"; join the table, insert another sql statement in the where clause or having clause can be the operator of a symbol, such as = >
< = 也可以文字的运算符 例如 LIKE IN BETWEEN exists用来测试内查询有没有产生任何结果,类似布尔值是否为真#如果有的话,系统就会执行外查询中的SQL语句,若是没有的话,那整个SQL语句就不会产生任何结果。语法:SELECT "字段1" from "表格1" where exists (SELECT * FROM "表格2" WHERE "条件");SELECT SUM(Sales) FROM Store_Info WHERE EXISTS (SELECT * FROM location WHERE Region = 'West'); 表链接 以下两表做实验 inner join(内连接): 只返回两个表中联结字段相等的行 left join(左连接):返回包括左表中的所有记录和右表中联结字段相等的记录 right join (右连接):返回包括右表中的所有记录和左表中联结字段相等的记录 使用子查询实现多表查询 create view 视图,可以当作是虚拟表或存储查询 视图跟表格的不同是,表格中有实际储存数据记录,而视图是建立在表格之上的一个架构,它本身并不实际储存数据记录。临时表在用户退出或同数据库的连接断开后就自动消失了,而视图不会消失。 视图不含有数据,只存储它的定义,它的用途一般可以简化复杂的查询。比如你要对几个表进行连接查询,而且还要进行统计排序等操作,写SQL语句会很麻烦的,用视图将几个表联结起来,然后对这个视图进行查询操作,就和对一个表查询一样,很方便。 视图表不会像临时表一样,断开连接后自动消失 视图表保存的是select 语句查询的结果,它本身不存储数据 当视图表的结构跟存储数据的原始表结构相同则可以修改或插入数据,如果不一样则不能修改或插入数据(比如多表连接查询的结果) union union:生成结果的数据记录值将没有重复,且按照字段的顺序进行排序 语法:[select 语句1] union [select 语句2]; union all:将生成结果的数据记录值都列出来,无论有多少重复 语法:[select 语句1] union all [select 语句2]; 交集值取两个SQL语句结果的交集select A.store_name from location A inner join store_info B on A.store_name=B.store_name;select A.store_name from location A inner join store_info B using(store_name); 两表其中的一个表没有指定的行,而另一个表这个行有重复不适用,要求两个表确实有交集的行的时候用 取两个SQL语句结果的交集,且没有重复 无交集值 显示第一个SQL语句的结果,且与第二个SQL语句没有交集的结果,且没有重复 case是SQL用来做为 if-then-else 之类逻辑的关键字语法:select casd("字段名") when "条件1" then "结果2" when "条件2" then "结果2" .... [else "结果N"] endfrom "表名";#条件可以是一个数值或公式。else 子句并不是必须的 空值(null) 和无值(’ ') 的区别 1.无值的长度为0,不占用空间的;而 NULL值的长度是NULL,是占用空间的。 2.IS NULL或者 IS NoT NULL,是用来判断字段是不是为NULL或者不是 NULL, 不能查出是不是无值的。 3.无值的判断使用=''或者''来处理。代表不等于。 4.在通过 count()指定字段统计有多少行数时,如果遇到NULL值会自动忽略掉,遇到无值会加入到记录中进行计算。 正则表达式匹配模式描述实例^匹配文本的开始字符‘^bd’ 匹配以bd开头的字符串$匹配文本的结束字符‘qn$’ 匹配以qn结尾的字符串.匹配任何单个字符's.t’匹配任何s和t之间有一个字符的字符串*匹配零个或多个在它前面的字符‘fo*t’ 匹配t前面有任意个o+匹配前面的字符1次或多次‘hom+’ 匹配ho开头,后面至少一个m的字符串字符串匹配包含指定的字符串‘clo’ 匹配含有clo的字符串p1|p2匹配p1或p2‘bg | fg’ 匹配bg或fg[…]匹配字符集合中的任意一个字符‘[abc]’ 匹配a或b或c[^…]匹配不在括号中的任何字符‘[ ^ ab]’ 匹配不含a或b的字符串{n}匹配前面的字符串n次‘g{2}’ 匹配含有2个g的字符串{n,m}匹配前面的字符串至少n次,至多m次‘f{1,3}’ 匹配f最少1次 最多3次 语法:select "字段" from "表名" where "字段" regexp {模式}; 存储过程 存储过程是一组为了完成特定功能的SQL语句集合。 存储过程在使用过程中是将常用或者复杂的工作预先使用SQL语句写好并用一个指定的名称存储起来,这个过程经编译和优化后存储在数据库服务器中。当需要使用该存储过程时,只需要调用它即可。存储过程在执行上比传统SQL速度更快、执行效率更高。 存储过程的优点: 1、执行一次后,会将生成的二进制代码驻留缓冲区,提高执行效率 2、SQL语句加上控制语句的集合,灵活性高 3、在服务器端存储,客户端调用时,降低网络负载 4、可多次重复被调用,可随时修改,不影响客户端调用 5、可完成所有的数据库操作,也可控制数据库的信息访问权限 创建存储过程delimiter $$ ##将语句的结束符号从分号;临时改为两个$$ 可以自定义create procedure proc() ##创建存储过程,过程名为proc,不带参数 ->Begin # # procedure body begins with the keyword begin-> select * from Store_info; # # procedure body statement-> end $$# procedure body ends delimiter with the keyword delimiter # # restore the closing symbol of the statement to the semicolon call proc # # call the stored procedure show create procedure [database.] Stored procedure name
Parameters of the stored procedure
In input parameter: indicates that the call passes a value to the procedure (the input value can be a literal or a variable)
Out output parameter: indicates that the procedure sends a value to the caller (multiple values can be returned) (outgoing values can only be variables)
Inout input and output parameters: indicates that the caller passes a value to the procedure, and that the procedure sends a value to the caller (the value can only be a variable)
View the stored procedure show create procedure [database.] The name of the stored procedure; the way to delete the contents of the stored procedure is to delete the original stored procedure and then create a new stored procedure with the same noun drop procedure if exists Proc;# delete only if it exists and do not add if exists, if the specified procedure does not exist, an error drop procedure proc1; control statement of the stored procedure is generated
(1) conditional statement if-then-else... End if
Delimiter $$create procedure proc2 (in num int) begin declare var int; set var=num*2; if var > = 10 then update t set id=id+1; else update t set id=id-1; end if; end$$
(2) Loop statement while... End while
These are all the contents of the article "what are the sql sentences in MySQL?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.