sql语句收集汇总
1,增加:
insert into 表名 (字段名1,字段名2,字段名3) values (值1,值2,值3);
示例:
(1,)
SQL.Text := 'INSERT INTO 工资表(月份,姓名,基本工资,个人社保代缴,个人公积金代缴,加班请假,销售提成,销售奖金,售后积分奖金,QQ群等级奖金,工资总额) VALUES ('+YueFen.QuotedString+','+KeFuAry[0].QuotedString+','+ KeFuAry[1]+',-'+KeFuAry[2]+',-'+KeFuAry[3]+','+KeFuAry[4]+','+KeFuAry[5]+','+ KeFuAry[6]+','+KeFuAry[7]+','+KeFuAry[8]+','+KeFuAry[9]+')';
(2,)
SQL.Add('insert into borrow (rno,bno,rdate) values ('''+edt1.text+''','''+edt2.text+''','''+formatdatetime('m''/''d''/''yyyy', date+15)+''')'); //插入一条借书记录,完成借书手续
2,修改:
update 表名 set 字段名='字段值';
示例:
(1,)
sql.text:= 'update 工资表 set 月份 ='+ YueFen.QuotedString;
(2,)
SQL.Add('update borrow set rdate=rdate+15 where rno='''+edt1.text+'''and bno='''+edt2.text+'''');
3,删除:
delete from 表名 where 字段1='值1'; //删符合条件的
delete from 表名;//全删
示例:
(1,)
SQL.Text := 'DELETE FROM 工资表' //删除全部
(2,)
procedure TForm6.btn2Click(Sender: TObject); //还书按钮
begin
with DataModule2.qry1return do
begin
close;
SQL.Text :='delete from borrow where rno =:rno and bno =:bno';//删除符合某个条件的记录
Parameters.ParamByName('rno').Value:=edt3.Text; //为rno参数赋值
Parameters.ParamByName('bno').Value:=DataModule2.qry1bnorno['bno']; //为bno参数赋值
ExecSQL; //删除借书记录,完成还书手续
DataModule2.qry1bnorno.Close;
DataModule2.qry1bnorno.Open; //重新显示读者还书后的借书信息
end;
end;
4,查询:
selete * from 表名 ;//查询表所有内容
selete 字段1,字段2 from 表名 ;//查询表某列;
selete * from 表名 order by 字段名 asc ; // 表按某列排序升序
selete * from 表名 order by 字段名 desc ; // 表按某列排序降序
selete * from 表名 where 字段名='值1'; //查询某值的记录
selete * from 表名 where 字段名 like '%值1%' ; //模糊查询
selete count(*) ,sum(字段1),sum(字段2*字段3)from 表名 ;// 统计字段值查询;
示例:
(1,)
SQL.Text := 'SELECT * FROM 需要做的事'; //查询所有记录
(2,)
SQL.Text := 'SELECT * FROM 公司利润表 ORDER BY '+Column.FieldName+' ASC'; //按升序排序
(3,)
SQL.Text := 'SELECT * FROM 公司利润表 ORDER BY '+Column.FieldName+' DESC';//按降序排序
(4,)
SQL.Add('select * from books where bname like ''%'+edt1.text+'%'''); //模糊查询
(5,)
//条件判断排序: case lst1.ItemIndex of //判断用户选择了第几项 0: SQL.Add('select * from books order by bno');//选择了第1项,按bno排序 1: SQL.Add('select * from books order by bname'); 2: SQL.Add('select * from books order by author') end;
(6,)
SQL.Add('select count(*),sum(num),sum(price*num) from books '); //统计列值查询
(7,)
SQL.Add('select * from borrow where rno='''+edt1.text+'''and bno='''+edt2.text+'''');//查询全部字段 符合某个条件的
(8,)
SQL.Add('select count(*)from borrow where bno='''+edt2.Text+''''); //查询符合条件的统计记录数
(9,)
SQL.Add('select bname,num from books where bno='''+edt2.text+''''); //查询符合条件的字段值
(10,)
SQL.Add('select count(*) as cnt from borrow where rno='''+edt1.Text+'''') ; //为统计字段立别名
注意:查询语句的执行语句都是 open;
其他增加、删除、修改的执行语句都是:ExecSQL;