Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql,加入和排除匹配项_Mysql_Database_Database Design - Fatal编程技术网

mysql,加入和排除匹配项

mysql,加入和排除匹配项,mysql,database,database-design,Mysql,Database,Database Design,我有一个系统,允许我处理书籍和人们在寻找书籍时可以做的研究 数据库是mysql,我有两个主表BOOKS和research BOOKS(id *int*, author *string*, title *string*, price *string*, tags *set('sci-fi', 'literature', 'theatre', 'drama', 'romance', 'recent', ...)*, ...) RESEARCHES is (researches_id, price

我有一个系统,允许我处理书籍和人们在寻找书籍时可以做的研究

数据库是mysql,我有两个主表
BOOKS
research

BOOKS(id *int*, author *string*, title *string*, price *string*, tags *set('sci-fi', 'literature', 'theatre', 'drama', 'romance', 'recent', ...)*, ...) 

RESEARCHES is (researches_id, price_max, author, ...)
当我在数据库中添加一本书时,我想得到与这本新书相匹配的研究。 由于人们可以在研究中使用许多标记(超过30个),我决定创建另一个表,而不是在
researches
中使用标记列,这将是一个
集合
, 因为我相信
集合
列上没有索引,所以我的数据库无法快速处理我的请求。

RESEARCHES_TAGS(id, researches_id, tag).
如果我添加一本p.K Dick的书,标签为“科幻”、“戏剧”,我想获得标签为
NULL
或标签为(“科幻”、“戏剧”)的研究,并排除 使用其他标签进行研究

因此,我正在执行以下请求:

SELECT * FROM RESEARCHES
LEFT JOIN RESEARCHES_TAGS ON RESEARCHES.researches_id = RESEARCHES_TAGS.researches_id AND RESEARCHES_TAG.tag IN ('literature', 'theatre', 'romance', ...)
WHERE RESEARCHES_TAGS.tag IS NULL
请注意,指定的标记数组不包含标记“sci-fi”、“戏剧”


我的问题是:有更好的解决方案吗?

您应该将您的
JOIN
标准与
WHERE
标准分开。另外,
内部连接对我来说更有意义,因为如果没有相应的
RESEARCHES
行,就不会有
RESEARCHES\u TAGS
行。。。对吧?

SELECT * FROM RESEARCHES
INNER JOIN RESEARCHES_TAGS ON RESEARCHES.researches_id = RESEARCHES_TAGS.researches_id 
WHERE RESEARCHES_TAGS.tag IS NULL OR tag IN ('literature', 'theatre', 'romance', ...)

还有。。。您应该澄清此查询,以指示标签属于哪个表。

我认为关键是要排除与标签“文学”、“戏剧”、“浪漫”等链接的行,并且只保留没有标准或具有匹配标准(科幻或戏剧或两者)的行。 您提出的查询正好相反

我自己通常使用
LEFT JOIN j,其中j.id为NULL
语句,但这显然是一种违反直觉的解决方案

也许重新定义表结构将允许更简单和/或更快的查询?让我们重新表述这个问题

Will asks for any products
Jane asks for products with at least A and B
John asks for products with at least C
Yael asks for products with at least A and C
Mark asks for products with at least A
您拥有的产品满足a和B要求。你要把它卖给谁

答案是威尔,简和马克,你如何把它放在表格和查询中


如果您只有几种类型的需求,您可以为每种需求设置一个字段,并要求
,其中C=0和D=0
,因为您不想联系需要您没有的东西的人。但是如果你有一个越来越多的标准清单呢?你不能一直添加字段。使用集合字段将排除使用索引的可能性。

您重新表述问题的方式非常好,符合我的担忧。事实上,我有一个不断增长的标准列表,我不能为每个标准创建一个列。那么,是的,如何以一种比“LEFT JOIN j WHERE j.id为NULL”语句更恰当的方式对Will、Jane和Mark进行研究呢?