teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

追記

2018/04/28 02:00

投稿

randr
randr

スコア202

title CHANGED
File without changes
body CHANGED
@@ -19,10 +19,15 @@
19
19
  |2|△△△△△|9|
20
20
  |3|☓☓☓☓☓|0|
21
21
 
22
+ **users**
23
+ |userid|username|auth|
24
+ |:--|:--|--:|
25
+ |0001|あいうえお|1|
26
+ |0002|管理者A|3|
22
27
 
23
28
  理想の結果
24
29
  ------
25
- ```pl/pgsql
30
+ ```SQL
26
31
  create or replace function codes(_kind varchar(10))
27
32
  returns table(****) as $$
28
33
  begin
@@ -50,6 +55,16 @@
50
55
  |2|△△△△△|9|確認完|
51
56
  |3|☓☓☓☓☓|0|未着手|
52
57
 
58
+ ```SQL
59
+ select * from users natural join code('auth');
60
+ ```
61
+
62
+ **上記SQLの結果**
63
+ |userid|username|auth|code_value|
64
+ |:--|:----:|:----:|--:|
65
+ |0001|あいうえお|1|一般|
66
+ |0002|管理者A|3|管理者|
67
+
53
68
  教えてほしいこと/やってみたこと
54
69
  ---
55
70
  - plpgsqlのtable(***)の部分に「_kind」の値を列名として指定することは可能でしょうか?

1

誤字脱字

2018/04/28 02:00

投稿

randr
randr

スコア202

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,7 @@
1
1
  テーブル
2
2
  ---
3
3
  **codelist**
4
- |kind|key|value|
4
+ |kind|code_key|code_value|
5
5
  |:--|:--:|--:|
6
6
  |status|0|未着手|
7
7
  |status|1|作業中|
@@ -24,14 +24,14 @@
24
24
  ------
25
25
  ```pl/pgsql
26
26
  create or replace function codes(_kind varchar(10))
27
- return table(****) as $$
27
+ returns table(****) as $$
28
28
  begin
29
- return query select key, value from codelist where kind = _kind;
29
+ return query select code_key, code_value from codelist where kind = _kind;
30
30
  end;
31
31
  $$language plpgsql;
32
32
  ```
33
33
  **codes('status')の結果**
34
- |status|value|
34
+ |status|code_value|
35
35
  |:--|--:|
36
36
  |0|未着手|
37
37
  |1|作業中|
@@ -44,7 +44,7 @@
44
44
  ```
45
45
 
46
46
  **上記SQLの結果**
47
- |id|title|status|value|
47
+ |id|title|status|code_value|
48
48
  |:--|:----:|:----:|--:|
49
49
  |1|○○○○○○|5|確認中|
50
50
  |2|△△△△△|9|確認完|