0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

テーブルの値を他のテーブルの値に置き換えて表示する

Last updated at Posted at 2018-10-23

環境

  • SQL Server 2014/2017

結論

値を参照できるように、両方のテーブルにIDが必要です。

example1.sql

select 置き換えたい列名 = (
    select 参照先列名
        from 参照先テーブル名
        where 置き換えたいテーブル名.置き換えたい列名 = 参照先テーブル名.参照先列名)
from 置き換えたいテーブル名

具体例

StudentsテーブルにはStudentID,Name,FavoriteSubjectIDの3つの列が存在します。

4.jpg

SubjectsテーブルにはSubjectID,SubjectNameの2つの列が存在します。

3.jpg

StudentsテーブルのFavoriteSubjectIDをSubjectテーブルのSubjectNameに置き換えて表示したいと思います。つまり、

  • これを
    4.jpg

  • こうしたい
    5.jpg

example2.sql

select StudentID,Name,FavoriteSubjectID =(
	select SubjectName 
		from Subjects 
		where Students.FavoriteSubjectID = Subjects.SubjectID)  
from Students

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?