Problem in sql query

I am facing some problem in a sql query

here is the scenario

I have 6 tables, where one of them is parent table and other are child table

Primary table has 62 attributes

Child table have 5 attribute entries

Parent table contain unique entry for each row,but child table may different multiple entries for each parent table entry,now i want to retrieve data from all the tables through single query

can anyone help me in this?

Replies

  • slashfear
    slashfear
    Hi Goyal,

    Really surprising none of them answered this question its easy buddy we have to use joins simple ok for you to understand this I have created 4 tables named demo1, demo2, demo3 and demo4. here the demo1 table has is the primary key (parent table) and the rest 3 are child which holds a reference to the parent table. following is the query to create a table for your reference:

    NOTE: I have done this in SYBASE database, the querys will be same in all the databases since it is SQL part 😉

    create table demo1(id int primary key, name varchar(20))
    create table demo2 (id int references demo1(id), prod varchar(20))
    create table demo3 (id int references demo1(id), prod3 varchar(20))
    create table demo4 (id int references demo1(id), prod4 varchar(20))



    And I populated the table as shown below:

    select * from demo1


    id name
    ----------- ----------------------------------------
    1 arvind
    2 slashfear


    select * from demo2


    id prod
    ----------- ----------------------------------------
    1 IPOD
    2 ACER LAPTOP
    1 ITOUCH


    select * from demo3

    id prod3
    ----------- ----------------------------------------
    1 BOOK
    1 PEN
    2 PENCIL


    select * from demo4

    id prod4
    ----------- ----------------------------------------
    2 something
    1 something again
    2 another prod


    Now to the join part which is shown below:


    select * from demo1 a inner join demo2 b on a.id = b.id join demo3 c on a.id = c.id join demo4 d on a.id = d.id

    id name id prod id prod3 id prod4
    ----------- ---------------------------------------- ----------- ---------------------------------------- ----------- ---------------------------------------- ----------- ----------------------------------------
    1 arvind 1 IPOD 1 BOOK 1 something again
    1 arvind 1 IPOD 1 PEN 1 something again
    1 arvind 1 ITOUCH 1 BOOK 1 something again
    1 arvind 1 ITOUCH 1 PEN 1 something again
    2 slashfear 2 ACER LAPTOP 2 PENCIL 2 something
    2 slashfear 2 ACER LAPTOP 2 PENCIL 2 another prod


    Simple right!! and sorry for the shabby output I dint have time to format the output!!

    If you have any doubts feel free to ask buddy...! 😉

    -Arvind

You are reading an archived discussion.

Related Posts

Hi all, I always wondered about the small cylindrical shaped thing in the end of laptop charging cable, mouse/keyboard cable etc...I thought it would be for some protection or something....
How can i add links in my signatures? Everyone is using but i can't 😕
i have done my engineering in computer sceince and now i was thinking of mphil..... i want a post graduation course that gives good package but is not that hard.........
Here are a few suggestions: 1. If it's a short distance that you have to travel, walk to your destination. Avoid using vehicles. 2. Don't use paper unless its very...
Hi all, I have a project using a pen drive. i want to store data in it, which can be read in any system but can't be formatted or over...