澄清两个同名的列

时间:2014-07-25 05:00:13

标签: c# mysql

我有这个问题:

SELECT CableNumber, CblType, ApprxmtLngth, ME.EquipmentNumber, ME2.EquipmentNumber, CblStatus, InstallMthd, S.PrdtCd FROM CableId C
 INNER JOIN MajorEquipment ME ON C.FromLoc = ME.MEId
 INNER JOIN MajorEquipment ME2 ON C.ToLoc = ME2.MEId
 INNER JOIN SupplierInfo S ON C.SupplierId = S.SupplierId WHERE
 (@CblType IS NULL OR CblType LIKE @CblType)
 AND (@CblStatus IS NULL OR CblStatus LIKE @CblStatus)
 AND (@FromLoc IS NULL OR ME.EquipmentNumber LIKE @FromLoc)
 AND (@ToLoc IS NULL OR ME2.EquipmentNumber LIKE @ToLoc);

当我将结果传递给模型时,如何定义哪个EquipmentNumber进入哪个变量?

while (mySqlReader.Read())
{
    CableID_Controller.CList.Add(new CableID_Model
    {
        CableNumber = Convert.ToString(mySqlReader["CableNumber"]),
        CableType = Convert.ToString(mySqlReader["CblType"]),
        SupplierPart = Convert.ToString(mySqlReader["PrdtCd"]),
        CableLength = Convert.ToInt32(mySqlReader["ApprxmtLngth"]),
        InstallMethod = Convert.ToString(mySqlReader["InstallMthd"]),
        Origin = Convert.ToString(mySqlReader["ME.EquipmentNumber"]),
        Destination = Convert.ToString(mySqlReader["ME2.EquipmentNumber"]),
        Status = Convert.ToString(mySqlReader["CblStatus"])
     });
}

因为ME.EquipmentNumberME2.EquipmentNumber不是标题的实际名称。

3 个答案:

答案 0 :(得分:2)

描述

只需在您的查询和代码中使用EquipmentNumber1运算符为列添加不同的别名(EquipmentNumber2as)。您还必须删除代码中的ME1ME2

示例

SELECT CableNumber, CblType, ApprxmtLngth, ME.EquipmentNumber as EquipmentNumber1, ME2.EquipmentNumber as EquipmentNumber2, CblStatus, InstallMthd, S.PrdtCd FROM CableId C
 INNER JOIN MajorEquipment ME ON C.FromLoc = ME.MEId
 INNER JOIN MajorEquipment ME2 ON C.ToLoc = ME2.MEId
 INNER JOIN SupplierInfo S ON C.SupplierId = S.SupplierId WHERE
 (@CblType IS NULL OR CblType LIKE @CblType)
 AND (@CblStatus IS NULL OR CblStatus LIKE @CblStatus)
 AND (@FromLoc IS NULL OR ME.EquipmentNumber LIKE @FromLoc)
 AND (@ToLoc IS NULL OR ME2.EquipmentNumber LIKE @ToLoc);

while (mySqlReader.Read())
{
    CableID_Controller.CList.Add(new CableID_Model
    {
        CableNumber = Convert.ToString(mySqlReader["CableNumber"]),
        CableType = Convert.ToString(mySqlReader["CblType"]),
        SupplierPart = Convert.ToString(mySqlReader["PrdtCd"]),
        CableLength = Convert.ToInt32(mySqlReader["ApprxmtLngth"]),
        InstallMethod = Convert.ToString(mySqlReader["InstallMthd"]),
        Origin = Convert.ToString(mySqlReader["EquipmentNumber1"]),
        Destination = Convert.ToString(mySqlReader["EquipmentNumber2"]),
        Status = Convert.ToString(mySqlReader["CblStatus"])
     });
}

更多信息

答案 1 :(得分:1)

通过向返回的列添加别名来修改查询:

SELECT CableNumber,
       CblType,
       ApprxmtLngth,
       ME.EquipmentNumber AS EquipmentNumber1,
       ME2.EquipmentNumber AS EquipmentNumber2,
       CblStatus,
       InstallMthd, [...]

然后,您可以分别引用EquipmentNumber1EquipmentNumber2

答案 2 :(得分:1)

你能否给列不同的aliases

  

SQL别名用于临时重命名表或列   标题。

这样的东西
SELECT 
 CableNumber, 
 CblType, 
 ApprxmtLngth, 
 ME.EquipmentNumber as MEEquipmentNumber, 
 ME2.EquipmentNumber as ME2EquipmentNumber,  
 CblStatus, 
 InstallMthd, 
 S.PrdtCd 
...

然后,您将在C#代码中使用这些别名。像

这样的东西
while (mySqlReader.Read())
{
    CableID_Controller.CList.Add(new CableID_Model
    {
        CableNumber = Convert.ToString(mySqlReader["CableNumber"]),
        CableType = Convert.ToString(mySqlReader["CblType"]),
        SupplierPart = Convert.ToString(mySqlReader["PrdtCd"]),
        CableLength = Convert.ToInt32(mySqlReader["ApprxmtLngth"]),
        InstallMethod = Convert.ToString(mySqlReader["InstallMthd"]),
        Origin = Convert.ToString(mySqlReader["MEEquipmentNumber"]),
        Destination = Convert.ToString(mySqlReader["ME2EquipmentNumber"]),
        Status = Convert.ToString(mySqlReader["CblStatus"])
     });
}