这些是我的表格:
Table_Bill_Summary
| Date | PersonId | BillNo |
Table_Bill_Items
| BillNo | ItemId | Quantity |
Item_MasterTable
| ItemId | ItemName | ItemRate |
我要指定一个PersonId。这就是我想要打印的内容:
SELECT *
FROM Table_Bill_Summary
WHERE PersonId=@X
(SELECT IM.ItemName,BI.Quantity,IM.ItemRate
FROM Table_Bill_Items AS BI
INNER JOIN Item_MasterTable AS IM ON BI.ItemId=IM.ItemId
WHERE BI.BillNo=??)
我的结果应如下:
23/04/2013 Person123 Bill32
Item20 23 100
Item21 20 200
Item23 1 300
23/04/2013 Person123 Bill39
Item2 2 100
Item11 40 800
Item43 1 700
我希望为Select
列上的父Select
查询检索的每一行运行子BillNo
查询
答案 0 :(得分:1)
select FLD1,FLD2,FLD3 from
(
SELECT CAST(Date as varchar(10)) as FLD1 ,CAST(PersonId as varchar(10)) as FLD2,
cast(BillNo as varchar(10)) as FLD3,1 as FLD4, Billno as fld5
FROM Table_Bill_Summary WHERE PersonId =@X
union
SELECT CAST(IM.ItemName as varchar(10)) as FLD1 ,CAST(BI.Quantity as varchar(10)) as FLD2,CAST(IM.ItemRate as varchar(10)) as FLD3 ,2 as FLD4, BS.Billno as fld5
FROM Table_Bill_Summary BS inner join Table_Bill_Items AS BI on bs.BillNo=BI.BillNo
INNER JOIN Item_MasterTable AS IM ON BI.ItemId=IM.ItemId
WHERE Bs.PersonId =@X
) as x order by fld5,fld4
答案 1 :(得分:0)
您无法在SQL中执行此操作。相反,通过获取按帐单排序的联接结果(帐单和项目)并在迭代结果集时跟踪上次查看的帐单来执行应用程序中的操作:
string stm = @"
SELECT BillNo, Date, ItemName, Quantity, ItemRate
FROM Table_Bill_Summary
JOIN Table_Bill_Items USING (BillNo)
JOIN Item_MasterTable USING (ItemId)
WHERE PersonId = @Person
ORDER BY BillNo
";
MySqlCommand cmd = new MySqlCommand(stm, conn);
cmd.Prepare();
cmd.Parameters.AddWithValue("@Person", person_id);
MySQLDataReader rdr = cmd.ExecuteReader();
bool isMore = rdr.Read();
while (isMore) {
int current_bill = rdr.GetInt32(0);
// output bill
do {
// output item
} while (isMore = rdr.Read() && rdr.GetInt32(0) == current_bill);
}
答案 2 :(得分:-1)
你做不到。 (至少没有一些奇怪的技巧;-) SQL select语句总是会产生一个显示相同列的行列表。
您通常可以使用报告软件或其他编程语言和GUI来解决此问题。