C# - 使用表别名时的SQLite错误:没有这样的列

时间:2018-05-30 01:46:25

标签: sql sqlite

我在SQLite Management Studio(2009版本)中运行以下查询,它运行正常并返回所需的结果集,但是当我从C#执行时,它给出了以下错误:

没有这样的列Q0.IntegrationItemCategoryLevelID。

似乎它无法看到"子查询中的表别名 - 我做了一些进一步的测试,它也看不到其他连接表(例如Q0)。我尝试将它分成两个查询,但它绝对杀死了性能。有没有人对如何解决这个问题有个好主意?

            SELECT DISTINCT Q1.IntegrationItemCategoryLevelID, A1.ShortDesc 
            FROM ((_Item I
            INNER JOIN (_ItemToItemCategory Q0 
                INNER JOIN _ItemCategory A0 ON A0.IntegrationItemCategoryID = Q0.IntegrationItemCategoryID) ON Q0.IntegrationItemID = I.IntegrationItemID)
            INNER JOIN (_ItemToItemCategory Q1 
                INNER JOIN _ItemCategory A1 ON A1.IntegrationItemCategoryID = Q1.IntegrationItemCategoryID) ON Q1.IntegrationItemID = I.IntegrationItemID)
            WHERE Q0.IntegrationItemCategoryLevelID = 14 AND A0.ShortDesc = 'LG05'
            AND Q1.IntegrationItemCategoryLevelID IN (9,4,5,7,10) ORDER BY Q1.IntegrationItemCategoryLevelID

编辑:删除了额外的括号,结果相同。

            SELECT DISTINCT Q1.IntegrationItemCategoryLevelID, A1.ShortDesc 
            FROM Item I
            INNER JOIN (ItemToItemCategory Q0 
                INNER JOIN ItemCategory A0 ON A0.IntegrationItemCategoryID = Q0.IntegrationItemCategoryID) ON Q0.IntegrationItemID = I.IntegrationItemID
            INNER JOIN (ItemToItemCategory Q1 
                INNER JOIN ItemCategory A1 ON A1.IntegrationItemCategoryID = Q1.IntegrationItemCategoryID) ON Q1.IntegrationItemID = I.IntegrationItemID
            WHERE Q0.IntegrationItemCategoryLevelID ='14' AND A0.ShortDesc = 'LG05'
            AND Q1.IntegrationItemCategoryLevelID IN (9,4,5,7,10) ORDER BY Q1.IntegrationItemCategoryLevelID

1 个答案:

答案 0 :(得分:1)

这是您的查询:

SELECT DISTINCT Q1.IntegrationItemCategoryLevelID, A1.ShortDesc 
FROM ((_Item I INNER JOIN 
       (_ItemToItemCategory Q0 INNER JOIN
        _ItemCategory A0
        ON A0.IntegrationItemCategoryID = Q0.IntegrationItemCategoryID
       )
       ON Q0.IntegrationItemID = I.IntegrationItemID
      ) INNER JOIN
      (_ItemToItemCategory Q1 INNER JOIN
       _ItemCategory A1
       ON A1.IntegrationItemCategoryID = Q1.IntegrationItemCategoryID
      )
      ON Q1.IntegrationItemID = I.IntegrationItemID
     )
WHERE Q0.IntegrationItemCategoryLevelID = 14 AND
      A0.ShortDesc = 'LG05' AND
      Q1.IntegrationItemCategoryLevelID IN (9, 4, 5, 7, 10)
ORDER BY Q1.IntegrationItemCategoryLevelID;

有点难以说出确切的错误是什么。我认为这是因为I被定义为最后一个JOIN的嵌套级别太多。

但是,这些都是内在的联系。并且您没有使用MS Access,因此您可以很好地重新排列它们:

SELECT DISTINCT Q1.IntegrationItemCategoryLevelID, A1.ShortDesc 
FROM _Item I INNER JOIN 
     _ItemToItemCategory Q0
     ON Q0.IntegrationItemID = I.IntegrationItemID INNER JOIN
     _ItemCategory A0
     ON A0.IntegrationItemCategoryID = Q0.IntegrationItemCategoryID INNER JOIN
     _ItemToItemCategory Q1
     ON Q1.IntegrationItemID = I.IntegrationItemID INNER JOIN
      _ItemCategory A1
     ON A1.IntegrationItemCategoryID = Q1.IntegrationItemCategoryID
WHERE Q0.IntegrationItemCategoryLevelID = 14 AND
      A0.ShortDesc = 'LG05' AND
      Q1.IntegrationItemCategoryLevelID IN (9, 4, 5, 7, 10)
ORDER BY Q1.IntegrationItemCategoryLevelID;

如果你有外连接,重新排列它们就不那么容易了。