会这样查询吗
能这样查询吗?
有一表A,如下
类别 数量
A 1
A 2
B 2
B 3
有一数组ay,元素为(A,B,C)。
如何根据ay得到如下查询结果:
类别 数量
A 3
B 5
C 0
多谢!!!
------解决方案--------------------
遍历ay
{
select * from A where 类别=ay[i]
在结果集中 sum一下数量
}
------解决方案--------------------
我直接上代码吧
最后输出的结果图:

------解决方案--------------------
select sum(数量) from table1 where 类别 in 类别字符串 group by 类别
类别字符串 由数组生成 格式为 数组元素1, 数组元素2,......数组元素n
有一表A,如下
类别 数量
A 1
A 2
B 2
B 3
有一数组ay,元素为(A,B,C)。
如何根据ay得到如下查询结果:
类别 数量
A 3
B 5
C 0
多谢!!!
------解决方案--------------------
遍历ay
{
select * from A where 类别=ay[i]
在结果集中 sum一下数量
}
------解决方案--------------------
我直接上代码吧
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, DB, ADODB;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
Button1: TButton;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure drawgrid(arr:array of string);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
const
arr:array[0..2] of string=('A','B','C');
begin
stringgrid1.RowCount:=Length(arr)+1;
drawgrid(arr);
end;
procedure TForm1.drawgrid(arr: array of string);
var
arr2:array of Integer;
i:Integer;
begin
SetLength(arr2,Length(arr));
StringGrid1.Cells[0,0]:='typename';
StringGrid1.Cells[1,0]:='num';
for i:=0 to Length(arr2)-1 do
begin
with ADOQuery1 do
begin
Close;
SQL.Text:=Format('select typename,SUM(number) as num from tableA where typename=%s group by typename',[QuotedStr(arr[i])]);
Open;
arr2[i]:=fieldbyname('num').AsInteger;
StringGrid1.Cells[0,i+1]:=arr[i];
StringGrid1.Cells[1,i+1]:=IntToStr(arr2[i]);
end;
end;
end;
end.
最后输出的结果图:
------解决方案--------------------
select sum(数量) from table1 where 类别 in 类别字符串 group by 类别
类别字符串 由数组生成 格式为 数组元素1, 数组元素2,......数组元素n