You can try something like this:
SELECT * FROM Tabela_Produtos p INNER JOIN Tabela_Filiais f ON f.id = p.filial_id
WHERE p.filial_id = 16 AND p.status LIKE 'Ativo' AND p.id NOT IN (
SELECT p2.id FROM Tabela_Produtos p2
INNER JOIN Tabela_Filiais f2 ON f2.id = p2.filial_id
WHERE p2.filial_id != 16 AND p2.status LIKE 'Inativo')
Whereas the data is all in one table MRL_PRODUTOEMPRESA
your consultation will stay:
SELECT * FROM MRL_PRODUTOEMPRESA
WHERE (EMPRESA = 16 AND STATUS LIKE 'A')
OR (EMPRESA != 16 AND STATUS LIKE 'I');
The query is simpler than the first, but be careful with the modeling of your database, because putting everything in the same table is not considered a good practice for relational databases and I do not advise, especially if it is used for professional projects.
There was no attempt on me for the detail inactive in ALL other companies. See if you can solve:
SELECT * FROM MRL_PRODUTOEMPRESA WHERE EMPRESA = 16 AND STATUS LIKE 'A' AND PRODUTO IN
(SELECT PRODUTO FROM MRL_PRODUTOEMPRESA WHERE EMPRESA != 16
AND STATUS LIKE 'I' HAVING COUNT(PRODUTO) >=
(SELECT DISTINCT COUNT(PRODUTO)-1 FROM MRL_PRODUTOEMPRESA GROUP BY PRODUTO)
GROUP BY PRODUTO)
- The above query searches all COMPANY 16 PRODUCTS with Active STATUS
- That are not on the list of COMPANIES other than 16 with Inactive Status
- Containing the total registered PRODUCTS minus 1 (referring to the single Active PRODUCT)
The assets you want are only those of the code 16 company? And that if simultaneously active (in this enterprise 16) is inactive at least in any other?
– Ismael
I want to fetch products that are Active only on 16 and simultaneously inactive in all others
– Celio Sartori
put the table structure in question, or some sample code that facilitates understanding.
– Aprendiz