如何在SQL中执行以下操作,访问?

时间:2013-04-03 18:58:59

标签: c# sql ms-access

我有以下查询

SELECT dictionaryEncryptedIndex, filelocation FROM  `DictionaryTable`
 WHERE  `dictionaryEncryptedWord` LIKE '0x0E6E'";

在c#中循环上述查询的结果,对于每次循环迭代,我使用以下查询来获得最终结果:

SELECT * FROM  `MaskedIndexTable` 
 WHERE  `maskedIndexEncryptedIndex`
  LIKE '" + dictionaryEncryptedIndexFROMABOVEQUERY + "'
   AND `fileLocation` = '" + filelocationFROMABOVEQUERY + "'";

dictionaryEncryptedIndex和maskedIndexEncryptedIndex之间的关系不是一对一的关系。

有没有人知道如何在一个可以在Microsoft Access中使用的SQL查询中执行上述操作?

我尝试过多种方式:

SELECT  * from DictionaryTable, MaskedIndexTable
  WHERE MaskedIndexTable.maskedIndexEncryptedIndex = DictionaryTable.dictionaryEncryptedIndex 
    AND  MaskedIndexTable.fileLocation =DictionaryTable.fileLocation
    AND `dictionaryEncryptedWord` LIKE '0x0E6E' 


SELECT dictionaryEncryptedWord, DictionaryTable.filelocation
  FROM DictionaryTable
 INNER JOIN MaskedIndexTable
    ON (MaskedIndexTable.maskedIndexEncryptedIndex =DictionaryTable.dictionaryEncryptedIndex  )
 WHERE  `dictionaryEncryptedWord` LIKE '...' 


SELECT distinct *
  FROM MaskedIndexTable
 INNER JOIN DictionaryTable 
    ON (MaskedIndexTable.maskedIndexEncryptedIndex = DictionaryTable.dictionaryEncryptedIndex  )
 WHERE   MaskedIndexTable.Id IN  
(
    SELECT DictionaryTable.Id
      FROM  DictionaryTable
     WHERE  `dictionaryEncryptedWord` LIKE '..') 
   AND  `dictionaryEncryptedWord` LIKE '...'

但它们似乎都没有产生正确的结果(我用c#代码得到的结果)

2 个答案:

答案 0 :(得分:0)

如果要在LIKE标准中使用通配符,则需要更改此项。

首先在Access中创建一个类似于第一个名为encryptedWordsQuery

的查询
SELECT DictionaryTable.dictionaryEncryptedIndex, DictionaryTable.fileLocation
FROM DictionaryTable
WHERE (((DictionaryTable.dictionaryEncryptedWord) Like "0x0E6E"));

其次在Access中创建一个查询,如下所示:

SELECT MaskedIndexTable.maskedIndex, MaskedIndexTable.maskedIndexEncryptedIndex, MaskedIndexTable.fileLocation
FROM MaskedIndexTable
WHERE Exists (SELECT * 
                FROM encryptedWordsQuery 
               WHERE MaskedIndexTable.maskedIndexEncryptedIndex = encryptedWordsQuery.dictionaryEncryptedIndex
                     AND MaskedIndexTable.fileLocation = encryptedWordsQuery.FileLocation);

我发现在Access中创建两个单独的查询更容易,而不只是一个。

答案 1 :(得分:0)

我认为这应该有用 - 如果你能够发布样本数据,我们可以制作一个SQLFiddle来演示它。

SELECT  dt.dictionaryEncryptedIndex, dt.filelocation 
FROM    DictionaryTable dt
    INNER JOIN MaskedIndexTable mit
        ON mit.fileLocation = dt.fileLocation 
            AND mit.maskedIndexEncryptedIndex = dt.dictionaryEncryptedIndex
WHERE   dt.dictionaryEncryptedWord LIKE '0x0E6E*'