SQL 查询中 AND 的问题
我有一个保存主题类型的表,另一个保存材料的表。然后我有另一个表,它保存每个表的键,从而创建多对多关系。
但是,当我尝试搜索主题以挑选两个共享相同材料的主题时,它不起作用。
示例表:
材料表:
MatID | Name
---------------
1 | book1
2 | note23
主题表:
TID | topic
---------------
1 | computer
2 | database
MatTop 表:
TID | MatID
------------
1 | 2
2 | 2
这是我的查询:
SELECT * FROM material
INNER JOIN mattop ON material.MatID = mattop.MatID
INNER JOIN topic ON mattop.TID = topic.TID
WHERE (topic.topic = 'computer') AND (topic.topic = 'database')
感谢您的帮助。
编辑 - 我知道 AND 是错误,抱歉。我的意思是如何让它输出具有与之相关的主题的材料。
I have a table that holds topic types another that holds materials. I then have another table which holds the keys of each table thus creating the many to many relation.
However, when I try to search the topics to pick out two topics which have share the same material it doesn't work.
Example tables:
Material Table:
MatID | Name
---------------
1 | book1
2 | note23
Topic table:
TID | topic
---------------
1 | computer
2 | database
MatTop table:
TID | MatID
------------
1 | 2
2 | 2
This is my query:
SELECT * FROM material
INNER JOIN mattop ON material.MatID = mattop.MatID
INNER JOIN topic ON mattop.TID = topic.TID
WHERE (topic.topic = 'computer') AND (topic.topic = 'database')
Thanks for any help.
EDIT - I know that the AND is the error sorry. I meant how do I get it to output the materials that have the topics associated with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题出在 where 子句中:
topic.topic 永远不能同时是“设计”和“注释”。
您的意思是让 AND 成为 OR 吗?
Your problem is in the where clause:
topic.topic can never be both 'Design' and 'Notes' at the same time.
Did you mean to have your AND be an OR?
要获得两个不同的主题,您需要加入两次。 (所以 Sonny Boy 在问题中是正确的,即 where 子句,但 OR 不是正确的答案)
类似的东西(注意我没有表,所以没有测试 SQL)
编辑注释:抱歉,我认为带有 2 个垫子的原始内容是正确的有一个版本只有一个,所以我已经恢复到原来的
这应该给出计算机和数据库主题中使用的材料。 where 子句中的简单 OR 将提供至少一个主题的材料,但不一定同时提供两个主题的材料
To get the two different topics in you need to join to it twice. (so Sonny Boy is correct in the problem is the where clause but OR is not the correct answer)
Something like (note I don't have the tables so not tested SQL)
Edit note : Sorry I think the original with 2 mattops is correct there was a version of this with only one so I have reverted to the original
This should give the materials used in both topics of computer and database. A simple OR in the where clause will give materials in at least one of the topics but not necessarily both
如果我理解正确的话,您想要共享相同材料的主题。
这样做的方法如下:
这将产生共享相同材料的主题对的列表。
因为 SQl 将表连接在一起,就像它们是 1 行/行一样,所以您不能同时拥有两个不同的主题值,除非您两次连接主题表
If I understood you correctly,you want topics that share the same material.
here's how you do that:
this would yield a list of pairs of topics that share the same material.
Because SQl joins the table together as if they were 1 row/line, you can't have two distinct topic values at the same time unless you join in the topic table twice