Question

I try to create a view with UNION ALL, and I need to have information about from which table records comes from. I tried to to this with type field:

  CREATE OR REPLACE VIEW 
    some_view
  AS
    SELECT
      "first_type" as type,
      t1.id,
      t1.author_id,
      t1.content,
    FROM t1
  UNION ALL
    SELECT
      "second_type" as type,
      t2.id,
      t2.author_id,
      t2.content,
    FROM t2

But it doesn't works with Posgresql. I saw this solution on the internet: "first_type" as type on the internet, so I believe it works in some database servers. How can I do this in Postgres?

Was it helpful?

Solution

In SQL, string/character literals (constants) need to be enclosed in single quotes, not double quotes.

"second_type" refers to a column named second_type you want 'second_type':

  CREATE OR REPLACE VIEW 
    some_view
  AS
    SELECT
      'first_type' as type,
      t1.id,
      t1.author_id,
      t1.content,
    FROM t1
  UNION ALL
    SELECT
      'second_type' as type,
      t2.id,
      t2.author_id,
      t2.content,
    FROM t2

For more details see the manual:http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top