how to write this sql statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • foo
    New Member
    • Feb 2007
    • 1

    #1

    how to write this sql statement

    i want to select a list of all user names and customer names. all names are in table 1 above. what does this sql statement look like?

    assume two tables:

    1. t1, columns id (unique generated value), and name (some char value)
    for example, the t1 contains
    1 customer name 1
    2 customer name 2
    3 user name 1 (at customer 1)
    4 user name 1 (at customer 2)
    5 user name 2 (at customer 1)

    2. t2, columns customer name (an integer) and user name (an integer)

    for example, the t2 contains
    1 3
    2 4
    1 5

    the sql statement i am looking for would show:

    customer id/customer name/user id/user name
    1/customer name 1/3/user name 1 (at customer 1)
    2/customer name 2/4/user name 2 (at customer 2)
    1/customer name 1/5/user name 2 (at customer 1)
  • michaelb
    Recognized Expert Contributor
    • Nov 2006
    • 534

    #2
    I would beg you to rethink this design.
    If I understand you correctly you have two entities: customers and users, and you need to maintain a relationship between them.

    One common approach would be to have two tables, customers and users.
    Then, depending on what type of relationship you need to maintain you will see whether you need yet another table.
    If you have one-to-one relationship all you need is to have foreign keys on these tables.
    If you have one-to-many or many-to-many relationship between customers and users, you should have the third table, something like
    customer_id integer not null,
    user_id integer not null

    Finally don't forget to create indexes to speed up your queries.

    Comment

    Working...