一、排序数据
1.1 为了明确排序使用select语句检索出的数据,可使用order by子句,order by 子句取一个或多个列的名字,并对数据进行排序。
select DeviceName from device order by LocationX;
二、按多个列排序
//对表中的两个列对结果进行排序--先按设备序号(DeviceNO),再按设备名称(DeviceName)
select DeviceNO,DeviceName,LocationX,LocationY from device order by DeviceNO,DeviceName;
三、指定排序方向
3.1 升序排序(A-Z);
//对表中的列对结果进行排序,按照LocationY 进行降序排列
select DeviceNO,DeviceName,LocationX,LocationY from device order by LocationY DESC;
先按照LocationY降序排序,在对LocationX排序
select DeviceNO,DeviceName,LocationX,LocationY from device order by LocationY desc, LocationX;
找到表中最大值或最小值(与limit结合使用)
select LocationX from device order by LocationX desc limit 1; //降序排列--最小值
select LocationX from device order by LocationX limit 1; //升序排列--最大值
四、过滤数据
4.1 使用where子句检索所需数据
//检索LocationX=1198的列名为DeviceNO,DeviceName,LocationX的数据
select DeviceNO,DeviceName,LocationX from device where LocationX=1198; //where应在from后;
//**在同时使用order by和 where字句时,应该让order by位于where之后,否则会报错!!!**
4.2 where子句操作符
**4.2.1 // = (等于);<>和!=(不等于);<(小于);<=(小于等于);>(大于);>=(大于等于);between(指定的两个值之间);
4.2.2 检查单个值;**
//检查设备序号为010的值
select DeviceNO,DeviceName,LocationX from device where DeviceNO=010; //where应在from后;
//检查LocationX小于1000的值
select DeviceNO,DeviceName,LocationX from device where LocationX<1000; //where应在from后;
4.3 不匹配检查
//LocationX不是550的;
select DeviceNO,DeviceName,LocationX from device where LocationX <> 550; //where应在from后;
4.4 范围值检查
//使用between检查某两个值范围内的数据
//LocationX 搜索1000-1200的值;
select DeviceNO,DeviceName,LocationX from device where LocationX between 1000 and 1200; //where应在from后;
4.5 空值检查
//null空值;null(无值)与字段包含0,、空字符串或仅仅包含空格不同
select DeviceNO,DeviceName,LocationX from device where LocationX is null; //where应在from后;这句话没有返回值,表明当前表中这样的行,故没有返回数据。