Java教程

sql 常用语句大全

本文主要是介绍sql 常用语句大全,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

排序:ORDER BY 语句用于根据指定的列对结果集进行排序。LIMIT x,y表示从x开始取y条数据,数据库是从第0条开始的。

SELECT * FROM table ORDER BY 列名 DESC LIMIT 0,1;  

 

选择最大最小值

select * from table where xxx  = (select max(列名)from table)

 

选择单独两列

select a,sum(b) c from table  选择a列起名为a  以及选择b加和起名为c 

 

整合:group by

group by job 把所有的job相同的进行合并

 

 

日期的分段: 直接选择YEAR就可以将日期的年份识别出来

YEAR(date) = '2025'

日期的比大小

data > '2025-1-1'

 

在几个选项中:in

product_name in ('C++','Java','Python')

同理不在就用not in

 

使用别名:as

SELECT * FROM 表名 AS 别名

 

内连接:inner join (和join相同)

 

 代表根据Person和Order相同的Id_P进行合并

 

合并计数:使用having来替代where,出现了count不能有where

count(xxx) as t from salaries group by xxx having t > 15

select number from grade group by number having count(number) >= 3;

 

一个表两个列拼接成一个列(mysql):

CONCAT_WS(space(1),last_name,first_name)   用一个空格进行连接 last_name + first_name 

 

插入多条语句:

insert into actor VALUES (1,'xxx','xxx'),(2,'xxx','xxx')

 

删除重复语句:

DELETE * from titles_test where id not in (select min(id) from titles_test Group by emp_no)

删除所有的行 where id 不在 (根据emp_no合并并选取最小的id)

 

替换语句(replace 和 update)

update用来更新表的内容,replace(列名,查找内容,替换成内容)

update titles_test set emp_no=replace(emp_no,'10001','10005') where id = 5 and emp_no = 10001

 

换表名: 

ALTER TABLE 表名 RENAME TO 新表名

ALter TABLE titles_test Rename To titles_2017

 

报错专栏:

SQL_ERROR_INFO: “You can‘t specify target table ‘titles_test‘ for update in FROM clause“

解决:把子查询表添加别名(select * from (select min(id) from titles_test Group by emp_no ) as a)

这篇关于sql 常用语句大全的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!