编写sql语句时需要注意的一些问题

来源:互联网 发布:gh0st源码分析 编辑:程序博客网 时间:2024/06/01 19:46
1、$和#的区别
     #表示字符串,$表示数值,在mapper.xml文件中,当传入的参数是数值型的时候,可以使用$接收参数,这时,select语句中不需要paremeterType这个属性。但一般情况下使用#,只有传入map时,使用$较多。
2、做批量插入时,需要循环集合,如<foreach>,在foreach中不需要加open="("和close=")",只需要在values后的字段上面加括号就行,在做批量删除时,可以加上open="("和close=“)”,因为open="("和close=“)”表示将所有的数据都放在这个括号里面。做批量删除的sql语句可以这样写:
     <delete id="deleteByKeys" parameterType="String">
        delete from enforcement_business_labour_worksite where id in (${value})
    </delete>
上述的批量删除传入的参数是String类型的,当传入的是Long类型时,需要这样写:
<delete id="deleteByKeys">
        delete from enforcement_business_labour_worksite where id in 
<foreach collection="idArray" item="idArray" open="" close="" separator=",">
#{idArray}
</foreach>
    </delete>
    这里${value} 是传过来的参数
3、sql语法集锦:
(1)在sql语句中可以写百分比,例如:
SELECTTOP 50 PERCENT * FROM Persons
从表中选取50%的记录,因为加了top,若有四条记录,会取前两条。
(2)sql语句中的模糊查询:
select lastName from table where City like (或者not like) '%lon%';
从表中查询city中有lon(或者没有lon)的;
(3)sql语句中的通配符(_、[charList]、[^charList])必须与like连用
sql中的通配符有_、[charList]、[^charList]或者[!charList]、%、
A: select * from table where name like '_ergo'
从表中选取名字的第一个字符之后是ergo;
B: select * from table where name like 'C_er_go';
从表中选取名字,该名字的组成规则是以C开头,之后是任意字符,之后是er,之后是任意字 符,再之后是go
C: select * from table where name like '[LAV]%';
从表中选取名字,该名字的组成规则是以LAV中的任意一个字符开始的;
D: select * from table where name like '[!LAV]%';
从表中选取名字,该名字的组成规则是不以LAV中的任何一个开头;
(4)in的用法
SELECT * FROM Persons WHERE LastName IN ('Adams','Carter')
从表中选取名字在Adams和Carter中的数据
(5)between..and
select * from table where name (not )between 'abc' and 'def'
从表中选取名字,该名字的组成规则是名字介于abc和def之间(或者在该范围之外),但两头是否包括需要根据具体的数据库定
id name city
1 abc ddd
2 fgdf vfg
3 gdfg gdfg
4 def fgtrg
选出来的数据是id为1,2,3的数据行
(6)union操作符
UNION 操作符用于合并两个或多个 SELECT 语句的结果集
使用union的多条select语句,必须拥有相同数量的列,并且这些列的数据类型需要一样,这些列的顺序需要一样。在默认情况下,union选取的是不同的列,如果允许数据重复,使用union all
(7) select * into new_table from old_table用法:
表示从旧表中复制数据到新表中,旧表不能存在,在做插入时会自动创建表
也可以在该语句中使用where和join on 语法
select * into new_table from old_table where City='beijing';
select * into new_table from old_table join Other_table on old_table.id=Other_table.id;
(8) sql中的date用法:
now():以2008-12-29 16:25:46方式输出时间
CURDATE():以2008-12-29方式输出时间
CIURTIME():以16:25:46方式输出时间
create table user{
OrderDate datetime NOT NULL DEFAULT NOW()
}
上面的建表语句会将当前时间自动输出
(9) NULL的用法(微软的用法)
处理表中的空值
SELECT ProductName,UnitPrice*(UnitsInStock+ISNULL(UnitsOnOrder,0)) FROM Products
表示如果UnitsOnOrder的值为空,则用0代替
IFNULL(Mysql的写法)
SELECT ProductName,UnitPrice*(UnitsInStock+IFNULL(UnitsOnOrder,0)) FROM Products
或者
SELECT ProductName,UnitPrice*(UnitsInStock+COALESCE(UnitsOnOrder,0)) FROM Products
(10)count的用法:
select count(column_name) from table;
表示返回该列的数据总数,null不计入,count(*)表示返回总的数据条数
select count(DISTINCT column_name) from table;
表示返回指定列的不同值的数目
(11) first函数的用法:
select first(column_name) from table;
first()表示返回指定的字段中第一个记录的值
(12)last函数的用法:
select last(column_name) from table;
last()表示返回指定字段中最后一个记录的值
(13)Having函数的用法:
having表示组,可以让我们筛选成组之后的数据
(14)having和where的区别:
WHERE语句在GROUPBY语句之前;SQL会在分组之前计算WHERE语句。 
HAVING语句在GROUPBY语句之后;SQL会在分组之后计算HAVING语句。
(15)mysql中的聚合函数:
sum,min,max,avg,count,在sql的执行顺序中聚合函数比having先执行,where 的优先级比聚合函数高,
(16)sql语句的执行顺序:
from...where...group by...having...select...order by
(17)concat和group concat的用法:
http://www.cnblogs.com/appleat/archive/2012/09/03/2669033.html
(18)修改表字段长度
ALTER TABLE news_draft MODIFY COLUMN news_img_url VARCHAR(1000)
(19)模糊查询中按时间段模糊查询:
传入的开始时间<表中的时间字段<传入的结束时间
原创粉丝点击