Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.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_Sql_Database_Search_Keyword - Fatal编程技术网

Mysql 仅使用给定关键字搜索,仅此而已

Mysql 仅使用给定关键字搜索,仅此而已,mysql,sql,database,search,keyword,Mysql,Sql,Database,Search,Keyword,我需要建立一个MySQL数据库系统,它将用于搜索我们可以用现有材料制作的产品 到目前为止,我对数据库结构有了一个想法,但我无法编写查询来选择我想要的内容 产品目录 +-------+-------------+ | id | product | +-------+-------------+ | 1 | product1 | +-------+-------------+ | 2 | product2 | +-------+-------------+

我需要建立一个MySQL数据库系统,它将用于搜索我们可以用现有材料制作的产品

到目前为止,我对数据库结构有了一个想法,但我无法编写查询来选择我想要的内容

产品目录

+-------+-------------+
|  id   |   product   |
+-------+-------------+
|   1   |   product1  |
+-------+-------------+
|   2   |   product2  |
+-------+-------------+
|   3   |   product3  |
+-------+-------------+
|   4   |   product4  |
+-------+-------------+
材料表

+-------+--------------+
|  id   |   material   |
+-------+--------------+
|   1   |   material1  |
+-------+--------------+
|   2   |   material2  |
+-------+--------------+
|   3   |   material3  |
+-------+--------------+
|   4   |   material4  |
+-------+--------------+
|   5   |   material5  |
+-------+--------------+
|   6   |   material6  |
+-------+--------------+
产品材料需求表(多对多)

由于库存控制等进一步改进,材料表需要单独使用。这些只是数据库结构的示例。对于这些类型的表和数据,系统需要执行搜索。而真正的数据库将使我更加复杂和拥挤

搜索示例

+----------------------------------------------+--------------------------+
|                   search                     |          result          |
+----------------------------------------------+--------------------------+
|  material1, material2, material3, material4  |    product1, product2    |
+----------------------------------------------+--------------------------+
|        material1, material2, material3       |          product2        |
+----------------------------------------------+--------------------------+
|  material3, material4, material5, material6  |    product3, product4    |
+----------------------------------------------+--------------------------+
|             material5, material6             |          product4        |
+----------------------------------------------+--------------------------+
|  material2, material3, material4, material5  |          product3        |
+----------------------------------------------+--------------------------+
|        material1, material2, material3       |          product2        |
+----------------------------------------------+--------------------------+
|        material4, material5, material6       |          product4        |
+----------------------------------------------+--------------------------+
|             material1, material2             |             -            |
+----------------------------------------------+--------------------------+
换句话说,搜索必须根据我们输入的材料名称给出产品名称的结果。目的是帮助我们决定如何利用现有的材料和知识

如果您有执行这种搜索的想法,请提前感谢。我愿意接受任何想法,包括数据库结构的改变。我只需要一个查询就可以了。我需要的不仅仅是你的想法,我还需要你的解释来理解

无论如何,谢谢。

这是一个“集合内集合”查询的示例。回答此问题最灵活的方法是使用聚合,并使用
having
子句:

select p.*
from TableOfMaterialNeeds tomn join
     Products p
     on tomn.productid = p.productid join
     TableOfMaterials tom
      on tomn.materialid = tom.materialid
group by p.productid
having sum(find_in_set(tomn.material, @LISTOFMATERIALS) = 0) = 0;
假设变量
@LISTOFMATERIALS
具有材质列表(例如
'material1,material2,…'
,逗号后没有空格)


find_in_set()
仅当物料不在集合中时才返回
0
sum()
统计物料的数量,
=0
表示没有物料。

我想您可能需要一个与产品ID关联的全文搜索列。
select p.*
from TableOfMaterialNeeds tomn join
     Products p
     on tomn.productid = p.productid join
     TableOfMaterials tom
      on tomn.materialid = tom.materialid
group by p.productid
having sum(find_in_set(tomn.material, @LISTOFMATERIALS) = 0) = 0;