I need to build a MySQL database system which is going to used for searching the product we can make with the materials we have.

So far, I have an idea for database structure but I couldn't write the query to select exactly what i want.

Table Of Products

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

Table Of Materials

+-------+--------------+
|  id   |   material   |
+-------+--------------+
|   1   |   material1  |
+-------+--------------+
|   2   |   material2  |
+-------+--------------+
|   3   |   material3  |
+-------+--------------+
|   4   |   material4  |
+-------+--------------+
|   5   |   material5  |
+-------+--------------+
|   6   |   material6  |
+-------+--------------+

Table Of Material needs For Products (Many To Many)

+-------+-------------+-------------+
|  id   |  productid  |  materialid |
+-------+-------------+-------------+
|   1   |      1      |      1      |
+-------+-------------+-------------+
|   2   |      1      |      2      |
+-------+-------------+-------------+
|   3   |      1      |      3      |
+-------+-------------+-------------+
|   4   |      1      |      4      |
+-------+-------------+-------------+
|   5   |      2      |      1      |
+-------+-------------+-------------+
|   6   |      2      |      2      |
+-------+-------------+-------------+
|   7   |      2      |      3      |
+-------+-------------+-------------+
|   8   |      3      |      3      |
+-------+-------------+-------------+
|   9   |      3      |      4      |
+-------+-------------+-------------+
|  10   |      3      |      5      |
+-------+-------------+-------------+
|  11   |      4      |      5      |
+-------+-------------+-------------+
|  12   |      4      |      6      |
+-------+-------------+-------------+

Table of materials need to be alone because of further improvements like stock control. These are just examples of data base structure. With these kind of tables and data, system needs to perform a search. And real database will me more complex and crowded.

Example Of Search;

+----------------------------------------------+--------------------------+
|                   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             |             -            |
+----------------------------------------------+--------------------------+

In words, search have to give the result with the product names based on the material names we give as input. The aim is help for deciding what we can do with the materials and knowledge we have.

Thanks in Advance if you have an idea to perform that kind of search. I am open to any idea including database structure change. I just need a query to work on. More than query I need your idea to work on, I need your explanation to understand.

Anyway, Thank You.

有帮助吗?

解决方案

This is an example of a "set-within-sets" query. The most flexible way of answering it is to use aggregation with a having clause:

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;

The variable @LISTOFMATERIALS is assumed to have a list of materials (such as 'material1,material2,...', no spaces after the comma).

The find_in_set() returns 0 only when a material is not in the set. The sum() counts the number of materials, and the = 0 is saying that there are none.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top