Search all records that exist in another table at least once

Asked

Viewed 81 times

1

Hello, I’m having trouble making the following query:

I have 3 tables: user_categories, main budget and orcamentos_new

When a user creates a budget, the system creates a record in main budget with the key information being: Who created the budget, completion date, status, etc.

On the table orcamentos_new is the specification of each item, being: item name (ex: inks), quantity, details and its category (paints => paint, for example) and the registration ID in main budget

The question is: in the table user_categories is safe from supplier categories, and this can be various types, such as paints, glass, lift etc

So what I need, is that when a buyer submit the quote (which may contain multiple categories) all suppliers (user_categories) that have at least ONE budget category, be returned in QUERY

So I have this:

usuarios_categorias
ID_user | ID_categoria
1         1 (tintas)
1         2 (vidros)
1         3 (elevador)


orcamentos_novo
Titulo  | ID_categoria
Vidros    2
Calhas    5
Piscina   8

Note that in the user_categories the user has the category Glass, but does not have the others, but is to return all records (Glass, gutters, pools) because it has at least one category

I’m not able to create a QUERY for this, what I currently have:

SELECT COUNT(*) as QTD_orc FROM orcamentos_principal
JOIN orcamentos_novo
ON orcamentos_novo.id_orcamento = orcamentos_principal.id
WHERE orcamentos_principal.status = 'ativo'

It searches all records but does not check if the user has at least one category

NOTE: I am the supplier, when I enter the page of available budgets he will have to fetch all budgets that I have at least ONE category, understand? then I need to return all the records of main budget where I have at least one of the categories of orcamentos_new associated with him

  • Can it be the solution with PHP, implode the array and reconsult with IN to check users with category? if I can solve so I make an answer.

  • @Anthraxisbr But in this case, he would seek ALL budgets and one by one would make another query ? thinking that there may be tens of thousands of budgets, it would bring me problems in the future, or not?

  • Therefore of implosion, and of using the IN, will be made a query only.

  • So please make an answer, thank you!

2 answers

2


Doing directly in the query would look like this:

SELECT *
  FROM orcamentos_principais op
 WHERE op.status = 'ativo'
   AND EXISTS(SELECT 1
                FROM orcamentos_novo on
                     INNER JOIN usuarios_categorias uc ON uc.id_categoria = on.id_categoria
               WHERE on.id_orcamento = op.id_orcamento
                 AND uc.id_user = 1) -- Aqui você substitui pelo seu ID
  • As I replied to the comment below, I did not explain right, I am the supplier, when I enter the available budgets page it will have to fetch all budgets that I have at least ONE category, understand? then I need to return all the records of main budget where I have at least one of the categories of orcamentos_new associated with him

  • @Evertonneri check the change I’ve made now

  • I adapted what I needed and it worked, thanks for the help! (I passed the user categories in the query instead of their id)

0

Supposing you made the inquiry and ordered the products:

produto - categoria
arroz   - comida
feifão  - comida

And this is in an array:

$arr = [
   1 => [
       'produto' => 'arroz',
       'categoria' => 'comida'
   ],
   2 => [
       'produto' => 'feijao',
       'categoria' => 'comida'
   ]
];

Implode the category column of the array into a string:

$categorias_cols = array_column($arr, 'categoria');

$categorias_str = implode('","', $categorias_col);

That will be the answer:

comida","comida

Concatene:

$categorias_str_ok = '"'.$categorias_str.'"';

Query by users using Mysql’s IN function:

'SELECT * FROM sua_tabela_de_usuarios WHERE campo_da_cagoria IN('.$categorias_str_ok.')';

This query will bring all users who have a relationship with the array categories.

I believe that this is what you need, has how to do directly in the query, but I won’t be able to say how.

  • So, not quite what I need, it will actually work, but in general, for what I need it: It will count how many budgets there are, how many I participate, how many finished and etc, in this way it will return users who have a budget category, and not the budget itself

  • It will return users, but you will have an array with the correct request ? you go through this array, compare 'category product' X 'user', if it is related, sum, otherwise do nothing, you get it ? or I didn’t get it right yet ?

  • Aaah I think I explained wrong, I am the supplier, when I enter the available budgets page it will have to fetch all budgets I have at least ONE category, understand? then I need to return all the records of main budget where I have at least one of the categories of orcamentos_new associated with him

  • Ah, I got it, so you do the same thing, get YOU, then implode your categories, new budget categories, and query with IN within budgets

Browser other questions tagged

You are not signed in. Login or sign up in order to post.