I have one table, say buchas containing a list of products. I had to create another table buchas_type with the type (class if you will) of the products in buchas.
Now I have to reference the class of product in buchas, by adding a type_id column.
So, my problem can be divided in two parts:
I need a subquery to "guess" the product type by its name. That should be somewhat easy, since the name of the product contains somewhere the name of its type. Example:
   -----------------Name----------------- | ---Type---
               BUCHA GOB 1600                   GOB
The problem is I have a type GOB(2) that will mess things up with the type GOB. (I have also other look alike types raising the same problem).
So far, I got this:
SELECT buchas_type.id 
FROM buchas_type 
  JOIN buchas ON buchas.description LIKE '%' || buchas_type.type || '%'
ORDER BY length(type) desc LIMIT 1;
That will solve the problem with the look alike types, since it returns the longest match. However, I need a WHERE clause otherwise I get always the same buchas_type_id. If I try for instance Where buchas.id = 50 I get a correct result.
The second part of the problem is the UPDATE command itself. I need to fill up the recently created buchas_type_id from the subquery I showed in (1). So, for every row in buchas, it has to search for the type in buchas_type using the current row's description as a parameter.
How to achieve that?
 
    