Do SQL query in 3 tables and return the value to another table?

Asked

Viewed 697 times

-1

I have 3 tables ( A, B, C ) with column descricao (with different records between them).

I want to do the SELECT of the three tables and send the result to another table D.

It would be something like this:

SELECT A.descricao, B.descricao, C.descricao FROM A, B, C JOIN D
  • send the result, would be: create a view, or save the data in another table?

  • Your question is not very clear. What is the structure of the tables? What do you mean by "send the result to another table D"?

  • For what was asked, the answer is from @Jorgeb. but I saw in the comments that you wanted one thing and asked another. I’m voting to close this one, but if you find out what you want, just ask a new question with the right doubt (I imagine you already solved, a year ago already).

2 answers

2

To insert the result of a SELECT in another table can do so:

INSERT INTO D (descricaoA, descricaoB, descricaoC)
SELECT A.descricao, B.descricao, C.descricao FROM A,B,C

Table D has the following format:

| descricaoA | descricaoB | descricaoC |
|------------|------------|------------|
|            |            |            |
  • 1

    I don’t understand this query. You are entering records in table D, I only need to recover the records (that already exist) of tables A, B, C and display in table D... With this query it returns null value to me!!!

  • Display in Table D? How so?

  • 1

    You enter a table, or update or delete it. I don’t know what you mean by "display".

  • "SELECT"; The values of tables A, B, C already exist...those values that are in each table in the column 'Description' has to be displayed in another table ( D ), ie a SELECT of these table, so I do not know mount the query for this

  • @davidsilva I don’t know what you mean by displayed in another table. For records to appear in another table they have to be inserted in that table.

  • I understand you my brother. but with this query the value is not displayed!

  • 1

    That one query is right. I can’t understand what it is to "display the values in table D".

  • You may be right yes, only in my case no. 'display' is just recover the data from the tables.

  • So you want to display in PHP, C#, etc? Display in another database table at least I’ve never heard of...

  • 1

    The displayed SQL is composed of two commands: a select, which takes the data from tables A, B and C, and an Insert, which uses the data returned by select to populate rows in table D, pairing the columns of select with the columns in table D. Done this, just give select in table D to get the results.

Show 5 more comments

0

You can create a view for this:

DROP VIEW IF EXISTS view_D;
CREATE VIEW view_D AS
SELECT descricao FROM tab_A
union
SELECT descricao FROM tab_B
union
SELECT descricao FROM tab_C;

And then select the data from this view:

Select * from view_D;

Browser other questions tagged

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