开发者

MySQL Basic about relationship

开发者 https://www.devze.com 2022-12-17 19:54 出处:网络
My doubt is about how to treat the follow thing: I have a system where a user belong to a company, and this user have their clients.

My doubt is about how to treat the follow thing:

I have a system where a user belong to a company, and this user have their clients.

How is the right way to get a list of all company clients and th开发者_如何学Pythone follow user name??

In the client table where i have a field with the one of this relations:

  • A company_id and user_id field
  • Just company_id field
  • Just user_id field cause user table have the company_id???
  • Something else...

Tkz Roberto


Tables relations:
Client (FK_companyId, FK_userId). "use FK_companyId only if you have multi-companies"
User (FK_companyId).
company (NO foreign keys for client or user).

if there is ONLY one company in the system then you don't need to include it in the relation:

SELECT clientInfo FROM client where userId=userSessionId;

if you have multi-companies then:
SELECT client.clientInfo,client.companyId,company.companyInfo FROM client left join company on (client.companyId = company.Id) where userId=userSessionId;

Note: the left join used to get the "company info" if its available but all user clients linked to that particular company will be retrieved.

Finally: If one client info can be managed by mutli users then you shall not link/couple the the two entities together.

BTW: your English is horrible!


You could use a select statement

SELECT company_id,user_id FROM client;
0

精彩评论

暂无评论...
验证码 换一张
取 消