如何在ms access中使用distinct
我有两张桌子。任务和类别。
TaskID 不是主键,因为存在重复值。当为特定任务选择多个联系人时,taskid 和其他详细信息将重复。我编写了查询:
SELECT Priority, Subject, Status, DueDate, Completed, Category
FROM Task, Categories
WHERE Categories.CategoryID=Task.CategoryID;
现在,由于该任务选择了多个联系人,因此对于taskid=T4,有两条记录(以灰色突出显示)。我曾尝试在 ms access 2003 中使用 unique,但它不起作用。我想显示不同的记录。 (这里不需要显示taskid)如果我写:
select priority, distinct(subject), .......
并保持与上面查询中提到的相同,那么它会给我一个错误。我也尝试过distinctrow,但没有成功。如何在ms access中获取不同的值?
I have two tables. Task and Categories.
TaskID is not a primary key as there are duplicate values.When there are multiple contacts are selected for a specific task,taskid and other details will be duplicated.I wrote the query:
SELECT Priority, Subject, Status, DueDate, Completed, Category
FROM Task, Categories
WHERE Categories.CategoryID=Task.CategoryID;
Now as multiple contacts are selected for that task,for the taskid=T4, there are two records(highlighted with gray). I have tried using distinct in ms access 2003 but its not working. I want to display distinct records. (Here there's no requirement to show taskid) If I write :
select priority, distinct(subject), .......
and remaining same as mentioned in above query then its giving me an error. I have tried distinctrow also.But didnt get success. How to get distinct values in ms access?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的。它是这样工作的。
Okay.Its working this way.
我不喜欢使用 SELECT DISTINCT,我发现它使我的代码需要更长的时间来编译。我这样做的另一种方法是使用 GROUP BY。
我目前没有 VBA,但这应该也可以。
I don't like using SELECT DISTINCT, I have found that it makes my code take longer to compile. The other way I do it is by using GROUP BY.
I do not have VBA up at the moment but this should work as well.
使用 SELECT DISTINCT 将适合您,但更好的解决方案是更改您的数据库设计。
重复记录可能会导致数据不一致。例如,假设具有相同 TaskID 的不同记录中有两种不同的状态。哪一个是正确的?
更好的设计将包括诸如任务表、联系人表和分配表之类的内容,如下所示(括号中的字段是 PK):
任务:[TaskID]、TaskPriority、Subject、Status、DueDate、Completed、StartDate、Owner , CategoryID, ContactID, ...
联系人:[ID], Name, Surname, Address, PhoneNumber, ...
分配:[TaskID, ContactID]
然后,您可以通过简单的 SELECT 从任务中检索任务表。
每当您需要知道分配给任务的联系人时,您都可以使用 JOIN 子句来实现,就像这样
或类似的。您可以使用 SQL 的所有查询功能对结果进行过滤、排序或分组。
Using SELECT DISTINCT will work for you, but a better solution here would be to change your database design.
Duplicate records may lead to inconsistent data. For example, imagine having two different status in different records with the same TaskID. Which one would be right?
A better design would include something like a Task table, a Contact table and an Assignment table, as follows (the fields in brackets are the PK):
Tasks: [TaskID], TaskPriority, Subject, Status, DueDate, Completed, StartDate, Owner, CategoryID, ContactID, ...
Contact: [ID], Name, Surname, Address, PhoneNumber, ...
Assignment: [TaskID, ContactID]
Then, you can retrieve the Tasks with a simple SELECT from the Tasks tables.
And whenever you need to know the contacts assigned to a Tasks, you would do so using the JOIN clause, like this
Or similar. You can filter, sort or group the results using all of SQL's query power.