简体   繁体   中英

MSSQL Select column values from one table to another table

How to retrive table(B) column values based on table(A) Primary key column value?in MSSQL

Table(A)

ID int PRIMARY KEY

NAME varchar(20)

Role_Id int

Table(B)

Role_Id int PRIMARY KEY,

Role_Type varchar(20).

I am confused about how to retrieve a column( Role_Type ) values from Table( B ) to Table( A ) by column( ID ).

Example,

Table(A)

ID =1,2,3.

NAME =john,joe,raj.

Table(B)

Role_Id =11,12,13,14,15,16,17,18.

Role_Type =A,B,C,D,E,F,G,H.

If i select ID=1 then i need below answer,

ID: 1

NAME: john

Role_Type: A,B,C,D,E,F,G,H

First thing is you have table A that have Role_Id colum.

ID int PRIMARY KEY

NAME varchar(20)

Role_Id int

That means that you will be only able to related one role to records on table A.

I think you need either to reformulated the question or create a new table C as a many to many relationship between table A and B that way you could have multiples Roles for the records on table A.

like: Table C Id Role_Id

Check the Microsoft reference for many to many relationship.

https://docs.microsoft.com/en-us/sql/analysis-services/multidimensional-models/define-a-many-to-many-relationship-and-many-to-many-relationship-properties?view=sql-server-2017

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM