由于“不同”原因导致访问查询备忘录字段被截断

发布于 2024-07-27 01:39:09 字数 455 浏览 3 评论 0原文

我在运行查询时遇到问题,既没有截断 NotesTbl 中的注释字段,也没有返回重复的条目。

对于 AccessTbl,UID 不是唯一的。 当我省略“不同”注释时,将多次返回,因为我在非不同条件下加入 AccessTbl。 当我使用 distict 时,注释字段会被截断,因为它是备注字段。

这是我的查询:

SELECT DISTINCT NotesTbl.pin, NotesTbl.noteid, NotesTbl.note, NotesTbl.date,
AccessTbl.affiliation, AccessTbl.name
FROM NotesTbl
LEFT JOIN AccessTbl
ON NotesTbl.UID = AccessTbl.UID
WHERE PIN = #pin#
AND UID = '#uid#'
ORDER BY NotesTbl.DATE DESC

I am having problems running a query without either truncating the note field in NotesTbl or returning repeated entries.

UID is not unique for AccessTbl. When I leave out "distinct" notes will return multiple times because I am joining with AccessTbl on a non-distinct condition. When I use distict, the note field is trunctated because it is a memo field.

Here is my query:

SELECT DISTINCT NotesTbl.pin, NotesTbl.noteid, NotesTbl.note, NotesTbl.date,
AccessTbl.affiliation, AccessTbl.name
FROM NotesTbl
LEFT JOIN AccessTbl
ON NotesTbl.UID = AccessTbl.UID
WHERE PIN = #pin#
AND UID = '#uid#'
ORDER BY NotesTbl.DATE DESC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

·深蓝 2024-08-03 01:39:10

Access 数据库引擎通常仅使用前 255 个字符来确定文本(“字符串”)数据的唯一性,这就是 DISTINCTGROUP BY 将被截断的原因。

这个建议有点啰嗦,但它确实有效:将 MEMO 分成 255 个字符的块,在这些块上使用 DISTINCT,然后再次将它们连接在一起,例如( Access 数据库引擎 ANSI-92 查询模式语法(即子查询的括号):

SELECT DT2.MyMemoCol_1 & DT2.MyMemoCol_2 AS MyMemoCol
  FROM (
        SELECT DISTINCT DT1.MyMemoCol_1, DT1.MyMemoCol_2
        FROM (
              SELECT MID(MyMemoCol, 1, 255) AS MyMemoCol_1, 
                     MID(MyMemoCol, 256, 255) AS MyMemoCol_2 
                FROM Test1
             ) AS DT1
       ) AS DT2;

已发布评论:

将备忘录分解为
255 个字符的块完全是
不必要。 您可以简单地排序
Left(MyMemoCol, 8192) 或其他
适当选择的值
返回字段长度。

嗯,根据我的测试,这根本不起作用。 快速重现:

CREATE TABLE Test1 (MyMemoCol MEMO NOT NULL);

INSERT INTO Test1 (MyMemoCol) VALUES (STRING(300, 'A'));
INSERT INTO Test1 (MyMemoCol) VALUES (STRING(300, 'A') & STRING(5, 'X'));
INSERT INTO Test1 (MyMemoCol) VALUES (STRING(300, 'A'));

SELECT LEFT$(MyMemoCol, 8192)
  FROM Test1
 GROUP 
    BY LEFT$(MyMemoCol, 8192);

在 SQL-92 查询模式下使用 Access2007 .accdb ACE 引擎查询对象的 SQL 视图进行测试,查询返回单行(不正确),其值已被截断为 255 个字符(不正确)。

早期的“分块”查询返回两行(正确),没有截断(正确)。

The Access database engine normally determines uniqueness of text ('String') data using only the first 255 characters and that is why DISTINCT and GROUP BY will truncate.

This suggestion is a bit long winded but it does work: split the MEMO into chunks of 255 characters, do use DISTINCT on the chunks, then concatenate them back together again e.g. (Access database engine ANSI-92 Query Mode syntax i.e. parens for subqueries):

SELECT DT2.MyMemoCol_1 & DT2.MyMemoCol_2 AS MyMemoCol
  FROM (
        SELECT DISTINCT DT1.MyMemoCol_1, DT1.MyMemoCol_2
        FROM (
              SELECT MID(MyMemoCol, 1, 255) AS MyMemoCol_1, 
                     MID(MyMemoCol, 256, 255) AS MyMemoCol_2 
                FROM Test1
             ) AS DT1
       ) AS DT2;

A comment has been posted:

Breaking the memo down in to
255-character chunks is entirely
unnecessary. You can simply sort on
Left(MyMemoCol, 8192) or some other
appropriately chosen value for the
field length returned.

Well, in my testing this doesn't work at all. Quick repro:

CREATE TABLE Test1 (MyMemoCol MEMO NOT NULL);

INSERT INTO Test1 (MyMemoCol) VALUES (STRING(300, 'A'));
INSERT INTO Test1 (MyMemoCol) VALUES (STRING(300, 'A') & STRING(5, 'X'));
INSERT INTO Test1 (MyMemoCol) VALUES (STRING(300, 'A'));

SELECT LEFT$(MyMemoCol, 8192)
  FROM Test1
 GROUP 
    BY LEFT$(MyMemoCol, 8192);

Tested using the SQL view of a Access2007 .accdb ACE engine Query object in SQL-92 Query Mode, the query returns a single row (incorrect) whose value has been truncated at 255 characters (incorrect).

The earlier 'chunking' query returns two rows (correct) without truncation (correct).

旧故 2024-08-03 01:39:10

我找到了一个似乎有效的解决方案。 我使用“分组依据”来强制 PIN 和 NoteID 的区别。 我尝试使用 First() 来从清晰度比较中排除注释以避免截断。

SELECT NotesTbl.pin, NotesTbl.noteid, First(NotesTbl.note) as notebody, NotesTbl.date,
AccessTbl.affiliation, AccessTbl.name
FROM NotesTbl
LEFT JOIN AccessTbl
ON NotesTbl.UID = AccessTbl.UID
WHERE PIN = #pin#
AND UID = '#uid#'
GROUP BY pin,affiliation,name,date,noteid
ORDER BY NotesTbl.DATE DESC

I found a solution that seems to work. I used a "group by" to force distinctness on the PIN and NoteID. I tried to exclude the note from distinctness comparissons by using First() to avoid truncation.

SELECT NotesTbl.pin, NotesTbl.noteid, First(NotesTbl.note) as notebody, NotesTbl.date,
AccessTbl.affiliation, AccessTbl.name
FROM NotesTbl
LEFT JOIN AccessTbl
ON NotesTbl.UID = AccessTbl.UID
WHERE PIN = #pin#
AND UID = '#uid#'
GROUP BY pin,affiliation,name,date,noteid
ORDER BY NotesTbl.DATE DESC
北方的巷 2024-08-03 01:39:10

编辑:--删除了第一个建议--

...
另一种方法是将您的请求分解为两个查询:一个查询优化 AccessTbl,以便 UID 在查询中是唯一的,另一个查询将 NotesTbl 联接到您刚刚创建的 qryAccessTblUnique 查询。

Edit: --Removed first suggestion--

...
Another method would be to break your request into two queries: One that refines AccessTbl so that UID is unique within the query, and another that joins NotesTbl to the qryAccessTblUnique query you just created.

诗笺 2024-08-03 01:39:10

您还可以过滤 CF 端的查询对象以删除(或忽略)已显示的响应。 效率不高,但如果您使用 Acess,我猜流量大并不是一个大问题。

类似这样的:

<cfoutput query="notes">
   <cfset diplay="true">
   <cfloop from="1" to="#notes.currentrow-1#">
       <cfif note neq notes.note[i]>
            <cfset display="false">
       </cfif>
    </cfloop>
    <cfif display>
       #note#
    </cfif>
</cfoutput>

对于大量的返回 n,这在 O(n^2) 下会很丑陋,但对于小 n,它应该给你你想要的。

You could also filter the query object on the CF end to remove (or ignore) responses which have already been displayed. Not efficient, but if you are using Acess, I'm guessing heavy traffic is not a huge issue.

Something like:

<cfoutput query="notes">
   <cfset diplay="true">
   <cfloop from="1" to="#notes.currentrow-1#">
       <cfif note neq notes.note[i]>
            <cfset display="false">
       </cfif>
    </cfloop>
    <cfif display>
       #note#
    </cfif>
</cfoutput>

For large numbers of returns n, this is going to be ugly at O(n^2), but for small n, it should give you what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文