【问题标题】:Get data from 2 table into 1 datagridview with condition将数据从 2 个表中获取到 1 个具有条件的 datagridview
【发布时间】:2017-12-20 20:27:38
【问题描述】:

你好,这是我的想法,但不知道如何正确地做到这一点。

Table1

ID   ID2     Name      Dosage
------------------------------
1    001     Name1     Dosage1
2    002     Name2     Dosage2
3    003     Name3     Dosage3


Table2

ID  Quantity   
------------------------
1   1000
2   2000
3   3000

查询类似:

从表 1 中选择 ID、名称、剂量和从表 2 中选择数量(从表 1 中相同的 ID) 其中 Table1 中的 ID2 ='002';

Datagridview Output

ID  Name    Dosage   Quantity
---------------------------------
2   Name2   Dosage2  2000

【问题讨论】:

  • 你需要一个join语句,然后将输出渲染成datagridvew;你用的是哪个数据库?

标签: c# sql database join


【解决方案1】:

一个简单的 SQL 连接应该可以工作。 试试这个:

select Table1.ID, Table1.Name, Table1.Dosage, Table2.Quantity
from Table1
inner join Table2 on Table2.ID = Table1.ID
where Table2.ID2 = '002';

【讨论】:

  • 先生,如果 ID2 在表 1 上怎么办?我试过 where Table1.ID2 =... 但它不起作用。
  • 它应该适用于 Table1.ID2 = '002' 错误信息是什么意思?
  • "一个在上下文中指定的非布尔类型的表达式,在 'where' 附近需要一个条件"。这是实际查询: string supID = "200049" -- "Select Supplier_productlist.ProductID , Supplier_productlist.Brand , Supplier_productlist.Dosage , Products.Quantity From Supplier_productlist inner join Products on Products.ProductID where Supplier_productlist.SupplierID = '" + supID + "'"
【解决方案2】:

从您给出的评论到另一个答案。您应该将其修复为如下所示:

DECLARE @SupID INT = 200049;

SELECT SP.ProductID, SP.Brand, SP.Dosage, P.Quantity
FROM Supplier_productlist AS SP
INNER JOIN Products AS P
ON P.ID = SP.ProductID
WHERE SP.SupplierID = @SupID;

如果SupplierID 是“字符串”,则改为这样声明:

DECLARE @SupID NVARCHAR(10) = '200049';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多