HELP - Join Selects

  • Please, I need resolv this problem.

    Table A TableC

    id_Menu(pk) id_User

    Name id_Menu (FK)

    Expected result, below

    Table Result

    id_Menu | id_Menu

    1 1

    2 null

    null 3

    4 4

    TSQL - I need to JOIN the result of the Selects

    Select TableA.id_menu

    INNER JOIN TableB

    ON TableA.id_Menu = TableB.id_Menu

    Where TableA.id_User = 1

    Select TableA.id_menu

    INNER JOIN TableB

    ON TableA.id_Menu = TableB.id_Menu

    Where TableA.id_User = 2

    It's possible?

    Sorry for my English

    Tks

    Fábio

    Brazil

  • Here are 2 options where option 2 is probably the better option:

    [font="Courier New"]SELECT

       TableA.id_menu

    FROM

       TableA INNER JOIN

       TableB ON

           TableA.id_Menu = TableB.id_Menu

    WHERE

       TableA.id_User = 1

    UNION ALL

    SELECT

       TableA.id_menu

    FROM

       TableA INNER JOIN

       TableB ON

           TableA.id_Menu = TableB.id_Menu

    WHERE

       TableA.id_User = 2

    --OR EVEN BETTER

    SELECT

       TableA.id_menu

    FROM

       TableA INNER JOIN

       TableB ON

           TableA.id_Menu = TableB.id_Menu

    WHERE

       TableA.id_User IN (1,2)[/font]

  • Tks for your help, but your query result in just one Field (id_menu), with all records to user 1 and user 2, and I need, if is possible, two Fields like id_Menu1 and id_Menu2, at the same time.

    Example.

    id_Menu1 | id_Menu2

    1 1

    null 2

    3 3

    4 null

    My result have 4 records. Now I cant view what records is null per user.

    The idea are compare the records of User1 vs User2.

    Tks.

  • All right, I was not sure what you wanted the first time, try this:

    [font="Courier New"]DECLARE @TableA TABLE(id_user INT IDENTITY(1,1) PRIMARY KEY, Name VARCHAR(10))

    DECLARE @TableB TABLE(id_user INT, id_menu INT)

    INSERT INTO @TableA

       (

       Name

       )

       SELECT

           'Jack'

       UNION ALL

       SELECT

           'Joe'

       UNION ALL

       SELECT

           'Steve'

       UNION ALL

       SELECT

           'Ron'

    INSERT INTO @TableB

       SELECT

           1,1

       UNION ALL

       SELECT

           1,2

       UNION ALL

       SELECT

           1,4

       UNION ALL

       SELECT

           2,1

       UNION ALL

       SELECT

           2,3

       UNION ALL

       SELECT

           2,4

    SELECT

       Menu_1,

       Menu_2

    FROM

        (SELECT

           B.id_menu AS menu_1

       FROM

           @TableA A INNER JOIN

           @TableB B ON

               A.id_user = B.id_user

       WHERE

           A.id_User = 1) AS A FULL OUTER JOIN

       (SELECT

           B.id_menu AS menu_2

       FROM

           @TableA A INNER JOIN

           @TableB B ON

               A.id_user = B.id_user

       WHERE

           A.id_User = 2) AS B ON

           menu_1 = menu_2

    ORDER BY

       COALESCE(menu_1, menu_2)[/font]

  • Yes, its run very well.

    First, I'm sorry for my english, I know it's horrible.

    Second, I'm very glad and grateful for your help.

    God Bless You!

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply