locked
Need a SQL Query RRS feed

  • Question

  • User1569151837 posted

    Hi,

    I have 2 Table s with following Colums

    Table 1 (Main table)

    qstnID(PrimaryKey)        Title       

    1                                         Question1

    2                                         Question2

    3                                         Question3

    4                                         Question 4

    5                                         Question 5

    6                                         Question 6

    7                                         Question 7

    Table 2

    ID (PrimaryKey)       qstnID (ForeignKey)         Title                            MainQstnID              

     1                                  7                                     Question 7                             2  

    2                                   5                                       Question 5                           1

    I need the result as   

    qstnID(PrimaryKey)        Title       

    1                                         Question1

    5                                         Question 5

    2                                         Question2

    7                                         Question 7

    3                                         Question3

    4                                         Question 4

    5                                         Question 5

    6                                         Question 6

    7                                         Question 7

    The reason behind this is, actually I am developing a feed back form for a particular project.  In this, different project may have different forms. Each forms have multiple questions. User need to first add forms in a table, then need to add questions for corresponding forms. all these data are keeps in different table. The new scenario is, to reduce the shear amount of forms for the user, they would like to load some questions based on a choice value. The reason for this is that a lot of Forms  are for the same thing, but the questions may vary on a single variable . For example, One of my question is a CheckBox format, say "A, B, C, D ". When the user checked B, there need to be display some more questiosn just below this question. If the user checked A, there is no need of display the additional queations. Thus, actually these additional questions also in the main question list, there is jus a look up for selection additional questions in a form. 

    So Table 1 is my Main Question table,. In this all questions are added. Table 2 is the details of my additional questions list. in this Main qstns ID also there

    Thursday, July 28, 2016 8:30 AM

Answers

  • User-698989805 posted

    Hello lijugmail! I've faced the same thing and here is the solution that I did:

    SELECT * FROM ((SELECT t1.qstnID, t1.Title FROM Table_1 t1)
    UNION
    (SELECT t2.MainQstnID, t2.Title FROM Table_2 t2 JOIN Table_1 t1 ON t1.qstnID = t2.qstnID))AS UNION_TABLE
    ORDER BY UNION_TABLE.qstnID

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 28, 2016 10:23 AM