//--如何写这样的sql语句?

nonheartboy 2003-04-03 09:18:51

void __fastcall TMainForm::PageControl1Change(TObject *Sender)
{
AnsiString sql;
sql="select * from cl_price,cl_amount where cl_amount.编号=:NO

group by cl_price.名称,cl_price.价格,cl_price.单位";

if(PageControl1->ActivePageIndex==1)
{
DBGrid2->DataSource=DataModule1->ClDataSource;
DataModule1->ClADOQuery->Close();
DataModule1->ClADOQuery->SQL->Clear();
DataModule1->ClADOQuery->SQL->Add(sql);
DataModule1->ClADOQuery->Parameters->ParamByName("NO")->Value=DataModule1->MainADOQuery->FieldValues["编号"];
DataModule1->ClADOQuery->Open();
}
}
这是一段小程序,MainADOQuery中有很多条记录,通过我上面这种查询方式,只能查询出一条记录,如果我想把和MainADOQuery中编号字段相同的记录从表cl_price,cl_amount中全部查询出来,用sql语句,应该怎么写?
...全文
22 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
my_hit 2003-04-04
  • 打赏
  • 举报
回复
可以这样子,select .... from ...where cl_amount.编号 in ( :str1,:str2,:str3......)
字符串可以用循环动态生成
kanhongsh 2003-04-03
  • 打赏
  • 举报
回复
void __fastcall TMainForm::PageControl1Change(TObject *Sender)
{
//假如两个表是用编号来进行连接
AnsiString sql;
sql="select cl_price.* from cl_price,cl_amount where cl_amount.编号=cl_price.编号 and cl_amount.编号=:NO

group by cl_price.名称,cl_price.价格,cl_price.单位";

if(PageControl1->ActivePageIndex==1)
{
DBGrid2->DataSource=DataModule1->ClDataSource;
DataModule1->ClADOQuery->Close();
DataModule1->ClADOQuery->SQL->Clear();
DataModule1->ClADOQuery->SQL->Add(sql);
DataModule1->ClADOQuery->Parameters->ParamByName("NO")->Value=DataModule1->MainADOQuery->FieldValues["编号"];
DataModule1->ClADOQuery->Open();
}
}
huang_jihua 2003-04-03
  • 打赏
  • 举报
回复
那你把 from 后面的表替换成你mainadoquery中的表就可以了。
比如:
mainadoquery中的查询语句是select * from cl_price,cl_amount where cl_amount.编号>'200301001'
那么就是:
sql="select cl_price.名称,cl_price.单位,cl_price.价格,sum(cl_amount.用量)"
"as 总量 from "
"(select * from cl_price,cl_amount where cl_amount.编号>'200301001'
) "
"where cl_price.编号 = cl_amount.编号 "
"group by cl_price.名称,cl_price.价格,cl_price.单位";
nonheartboy 2003-04-03
  • 打赏
  • 举报
回复
你这样不是把我数据库中的全部的记录都弄上去了吗?
我还要实现选择一些记录(mainadoquery中有的),进行查询
huang_jihua 2003-04-03
  • 打赏
  • 举报
回复
你可以用我第一个方法阿,不用传参数进去了。
sql="select cl_price.名称,cl_price.单位,cl_price.价格,sum(cl_amount.用量)"
"as 总量 from "
"from cl_price,cl_amount "
"where cl_price.编号 = cl_amount.编号 "
"group by cl_price.名称,cl_price.价格,cl_price.单位";
nonheartboy 2003-04-03
  • 打赏
  • 举报
回复
huang_jihua(不懂...学习) :
您还没有明白我的意思,我要查询的满足条件(即:和MainADOQuery所有的“编号”字段相同的记录,其中MainADOQuery中的记录是不定的,记录数量也是不确定的,是随机生成的)的,
这些字段{cl_price.名称,cl_price.单位,cl_price.价格,cl_amount.用量 as 总量 }

我的代码只是实现了一条记录的(一个编号)
如何实现多条记录(多个编号)的?
huang_jihua 2003-04-03
  • 打赏
  • 举报
回复
你是要查出所有编号的记录吗?是的话那你就不要过滤编号了,这样:
sql="select cl_price.名称,cl_price.单位,cl_price.价格,sum(cl_amount.用量)"
"as 总量 from "
"from cl_price,cl_amount "
"where cl_price.编号 = cl_amount.编号 "
"group by cl_price.名称,cl_price.价格,cl_price.单位";
如果你是要查一个编号的所有记录+他们的合计值:就这样
sql="select cl_price.名称,cl_price.单位,cl_price.价格,cl_amount.用量"
"as 总量 from "
"(select * from cl_price,cl_amount where cl_amount.编号=:NO) "
"where cl_price.编号 = cl_amount.编号 "
    "union all "
"select cl_price.名称,cl_price.单位,cl_price.价格,sum(cl_amount.用量)"
"as 总量 from "
"(select * from cl_price,cl_amount where cl_amount.编号=:NO) "
"where cl_price.编号 = cl_amount.编号 "
"group by cl_price.名称,cl_price.价格,cl_price.单位";

nonheartboy 2003-04-03
  • 打赏
  • 举报
回复
huang_jihua(不懂...学习) :
完整的sql语句是这个样子的:
sql="select cl_price.名称,cl_price.单位,cl_price.价格,sum(cl_amount.用量)"
"as 总量 from "
"(select * from cl_price,cl_amount where cl_amount.编号=:NO) "
"where cl_price.编号 = cl_amount.编号 "
"group by cl_price.名称,cl_price.价格,cl_price.单位";
这样的动态查询只能是查出一个编号的记录,查不出全部得来。分组语句和这没关系,也不能去掉。
huang_jihua 2003-04-03
  • 打赏
  • 举报
回复
首先你要知道cl_price,cl_amount两个表有什么样的关系,
比如:cl_amount.编号=cl_price.编号或其他
然后你要把这个关系加到WHERE语句后面
还有就是你要全部查询出来可以把分组的语句去掉。
nonheartboy 2003-04-03
  • 打赏
  • 举报
回复
sharkxie(tarzon) :
呵呵,说的不严谨,应该是MainADOQuery连接的数据库的表中有多条记录
我想你应该能明白我的意思
sharkxie 2003-04-03
  • 打赏
  • 举报
回复
什么叫MainADOQuery中有很多条记录?

1,178

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 数据库及相关技术
社区管理员
  • 数据库及相关技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧