By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,619 Members | 2,698 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,619 IT Pros & Developers. It's quick & easy.

how to write this sql statement

P: 1
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)
Feb 21 '07 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 534
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.
Feb 21 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.