subconsulta with 3 different tables using ilike in postgres

Asked

Viewed 137 times

0

I’m having a question. How do I search a given text using ilike to know if this text exists in 3 related tables. ? I tried to exist but it did not give

CREATE TABLE metodo (
    id SERIAL NOT NULL,
    descricao CHARACTER VARYING NOT NULL
);
CREATE TABLE forma (
    id SERIAL NOT NULL,
    id_metodo INTEGER,
    descricao CHARACTER VARYING NOT NULL
);
CREATE TABLE acao (
    id SERIAL NOT NULL,
    id_forma INTEGER,
    descricao CHARACTER VARYING NOT NULL
);
  • Show table structure and text fields.

  • sql CREATE TABLE metodo ( id SERIAL NOT NULL, Description CHARACTER VARYING NOT NULL ); CREATE TABLE forma ( id SERIAL NOT NULL, id_metodo INTEGER, Description CHARACTER VARYING NOT NULL ); CREATE TABLE acao ( id SERIAL NOT NULL, id_forma INTEGER, Description CHARACTER VARYING NOT NULL ); sql

1 answer

1


How about using the UNION:

SELECT 'M', id, descricao FROM metodo WHERE descricao ILIKE '%texto%'
UNION
SELECT 'F', id, descricao FROM forma WHERE descricao ILIKE '%texto%'
UNION
SELECT 'A', id, descricao FROM acao WHERE descricao ILIKE '%texto%'

Browser other questions tagged

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