A
A
ars-bars2018-09-06 10:16:58
SQL
ars-bars, 2018-09-06 10:16:58

How to search in SQL database by groups?

Hello!
There is a SQL database with the following structure:
5b90d2078d31a271650719.png
Alloy grades are stored in the database. Each brand has a list of chem. elements, as well as the minimum and maximum content of these elements in this brand.
Further introductory: there are results by elements, for example:
Ag - 0.007
B - 0.002
Fe - 0.06
Cu - 0.01
How to create a search for brands in the database to determine which brand they correspond to?
There is no Mg in the input, but we must nevertheless receive an answer - Entry No. 2.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Entelis, 2018-09-06
@ars-bars

select 
   name
from table
where 
   (element = "Ag" and Min <= 0.007 and 0.007 <= Max)
   OR
   (element = "B" and Min <= 0.002 and 0.002 <= Max)
   OR
   (element = "Fe" and Min <= 0.06 and 0.06 <= Max)
   OR
   (element = "Mg" and Min <= 0.025 and 0.025 <= Max)
   OR
   (element = "Cu" and Min <= 0.01 and 0.01 <= Max)
group by Name
having count(ID) = 5

where 5 is the number of conditions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question