0

I know this question may sound confusing, but let me try to simplify it.

I have a query, let's call query1, and a much larger table of all products. Here is query1:

Item_Code    Description    Order_Qty    Option
1000         Prod1          5            Blue
1005         Prod5          3            Brown
1602         Prod6          1            Red
5620         Prod8          6            Yellow
9865         Prod2          1            Brown
1624         Prod3          3            Brown
9876         Prod12         4            Blue

Now in my table, I have a much bigger list of products with the same format. I want to make a new query that contains ALL that are blue, brown, red, and yellow. It works, but there is always a duplicate.

I'm not sure how to post my attempt but I'll explain what I tried. I made a new query and included the table and query1. I made a relationship between the two, to include only rows where the "option" is equal. But for some reason the resulting query will come out with repeats. Such as:

Item_Code    Description    Order_Qty    Option
1000         Prod1          5            Blue
1009  <--    Prod2          6            Blue    
1009  <--    Prod2          6            Blue    
1010  <--    Prod9          7            Blue    
1010  <--    Prod9          7            Blue    
1011  <--    Prod11         9            Blue    
1011  <--    Prod11         9            Blue    
9876  <--    Prod12         4            Blue    
9876  <--    Prod12         4            Blue
1005  <--    Prod5          3            Brown
1005  <--    Prod5          3            Brown
9865  <--    Prod2          1            Brown
9865  <--    Prod2          1            Brown
1624  <--    Prod3          3            Brown
1624  <--    Prod3          3            Brown
9877         Prod99         7            Brown
1111  <--    Prod67         8            Brown   
1111  <--    Prod67         8            Brown    
1602         Prod6          1            Red
1752         Prod56         2            Red
5620         Prod8          6            Yellow

And the worst part is, it won't always repeat. Maybe I'm approaching it wrong.

I know this may be a case of tldr, but if anyone could help that would be great.

Thanks.

4

3 回答 3

2

听起来您需要在查询中添加GROUP BY子句或DISTINCT关键字。

SELECT DISTINCT Item_Code, Description, Order_Qty, Option

(或单击 Access 中执行相同操作的任何选项。)

鉴于您提供的信息,无法诊断查询返回“重复”行的原因。

当 中的行与 中all_products的两个或更多行匹配时,查询将返回“重复”行,因此您从中的每个匹配行query1获取该行的副本。all_productsquery1


如果您Option从 query1 获得不同的列表,您可能会获得更好的性能。(我并没有真正“做”MS Access。我提出这个问题是因为它被标记为“mysql”。Jet 数据库引擎很酷,但它在多用户环境中不能很好地工作。)在 SQL 中服务器、甲骨文、MySQL、DB2、Teradata 等。我们会像这样编写查询:

SELECT p.Item_Code
     , p.Description
     , p.Order_Qty
     , p.Option
  FROM mytable p
  JOIN ( SELECT q1.Option
           FROM query1 q1
          GROUP BY q1.Option
       ) q
    ON q.Option = p.Option
于 2013-08-13T21:59:01.340 回答
2

您的查询返回重复项,因为您可能只从一个表中选择列,但您的连接不够具体,因此生成的笛卡尔积会使您的结果相乘。即,因为您唯一的加入option = option意味着每个人都blue将加入其他人blue。您可能希望在连接中的 ON 子句中有更多限制。

使用distinct可能会使它看起来像正确的答案,但你只是掩盖了问题。

于 2013-08-13T22:07:40.783 回答
0

如果我正确理解您的问题,您想使用 Query1 作为一种查找表来从 Table.xml 中选择行。

所以你可以这样做:

select * from table
where color in 
(select color from query1)

如果您想根据 2 个字段制定选择标准,您可以执行以下操作:

select * from table
where color1&color2 in 
(select color1&color2 from query1)

如果你想寻找遗漏,你可以这样做:

select * from table
where color not in 
(select color from query1)

请注意,这仅从表中选择数据。它不会在 table 和 query1 之间进行任何连接。

我认为其他人是正确的。您在连接中得到奇怪的重复项,因为每个表中的连接项都有多行。

于 2018-10-19T06:22:01.770 回答