简体   繁体   English

将基于内容的行与Access SQL中的其他表行进行比较

[英]Comparing row based on contents to another tables rows in Access SQL

Hi I'm working on a project and for time constraint reasons I need to keep working in Access which may be the root of all my problems but maybe there's hope. 嗨,我正在从事一个项目,由于时间限制,我需要继续在Access中工作,这可能是我所有问题的根源,但也许还是有希望的。

I have a database that includes a table ANSWERS filled with input for users "wants" there are multiple columns which each correspond to an answer to a different question asking if they, Don't Care, Want, or Need something. 我有一个数据库,其中包含一个表ANSWERS,其中填充有用户“想要”的输入,每行都有多个列,每个列对应于一个不同问题的答案,询问他们是否在乎,不需要或需要什么。

EG: Answers: EG:答案:

    Bacon  |  Ham   |  Sausage
  ________________________________
1     0        0          2
2     2        1          0
3     0        2          0
4     1        1          1

(0 = Don't Care, 1 = Want, 2 = Need) (0 =无关,1 =想要,2 =需要)

I want to compare a row from table Answers to the Available table. 我想将表Answers中的一行与Available表进行比较。

EG: Available: EG:可用:

    Bacon  | Ham    |  Sausage
   ________________________________
1    0        1           0
2    0        0           0
3    1        1           1
4    1        1           0

(0 = Unavailable, 1 = Available) (0 =不可用,1 =可用)

So I would want to compare row 1 from Answers to Available so because row 1 includes sausage=2 then I would want to receive row 3 from Available because sausage=1. 因此,我想将“答案”与“可用”中的第1行进行比较,因此,因为第1行包含“香肠” = 2,所以我想从“可用”中接收第3行,因为“香肠” = 1。

I'd be happy receiving the entire row, or the row ID and a "1" for the rows being a match. 我很乐意收到整行,或者行ID和匹配的行的“ 1”。

Ultimately I'd need to do this for all each of the rows in Answers. 最终,我需要对“答案”中的所有每一行都执行此操作。

Any ideas are appreciated, I was thinking using Intersect might work but since that doesn't work in access. 任何想法都值得赞赏,我以为使用Intersect可能有效,但是由于在访问中无效。 I've also considered joining the tables, I could also change data variables or formats if necessary. 我也考虑过加入表格,如有必要,我也可以更改数据变量或格式。

Thanks very much 非常感谢

Edit: Don't Care was previously Don't Want. 编辑:不在意以前是不要。 Changed for clarity. 为清楚起见进行了更改。

Give this a try: SELECT tblAnswers.UserID, IIf([tblAnswers].[bacon]>0 And [tblMenus].[Bacon]<>0,[MenuID],Null) AS BaconMenu, IIf([tblAnswers].[Ham]>0 And [tblMenus].[Ham]<>0,[MenuID],Null) AS HamMenu, IIf([tblAnswers].[Sausage]>0 And [tblMenus].[Sausage]<>0,[MenuID],Null) AS SausageMenu FROM tblAnswers, tblMenus WHERE (((IIf([tblAnswers].[bacon]>0 And [tblMenus].[Bacon]<>0,[MenuID],Null)) Is Not Null)) OR (((IIf([tblAnswers].[Ham]>0 And [tblMenus].[Ham]<>0,[MenuID],Null)) Is Not Null)) OR (((IIf([tblAnswers].[Sausage]>0 And [tblMenus].[Sausage]<>0,[MenuID],Null)) Is Not Null)); 试试看: SELECT tblAnswers.UserID, IIf([tblAnswers].[bacon]>0 And [tblMenus].[Bacon]<>0,[MenuID],Null) AS BaconMenu, IIf([tblAnswers].[Ham]>0 And [tblMenus].[Ham]<>0,[MenuID],Null) AS HamMenu, IIf([tblAnswers].[Sausage]>0 And [tblMenus].[Sausage]<>0,[MenuID],Null) AS SausageMenu FROM tblAnswers, tblMenus WHERE (((IIf([tblAnswers].[bacon]>0 And [tblMenus].[Bacon]<>0,[MenuID],Null)) Is Not Null)) OR (((IIf([tblAnswers].[Ham]>0 And [tblMenus].[Ham]<>0,[MenuID],Null)) Is Not Null)) OR (((IIf([tblAnswers].[Sausage]>0 And [tblMenus].[Sausage]<>0,[MenuID],Null)) Is Not Null));

Just paste that into a SQL view make Query window (After changing the table and column names to match yours) You will obviously need to tweak it as reality needs, but it does what you asked for with the data provided. 只需将其粘贴到SQL视图中的“查询”窗口中(更改表名和列名以使其与您的名称相匹配),显然您将需要根据实际需要对其进行调整,但是它确实可以满足您对提供的数据的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM