我可以像这样查询字段数据类型:
SELECT * FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'table-name';
这样我就可以在查询表时根据正确的数据类型获取用于构建用户界面元素的数据类型。
但我的问题是,当我在其他(相关)表上使用子查询并将 foreign_keys 作为整数时,我不能再使用 information_schema 定位数据类型,因为我需要定位外键表。并且 data_type 不再匹配(因为 foreign_key 可能返回字符串、日期等)。
如何更改查询以从相关表中获取相应的正确数据类型:
SELECT the-returned-data-type-from-the-foreign-key-relation FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'table-name';
SELECT
some_table.id,
some_table.somefield,
(SELECT another_table.somefield FROM another_table WHERE another_table.id = some_table.foreign_key LIMIT 1) AS some_value,
FROM some_table
INNER JOIN another_table
ON some_table.foreign_key = another_table.id;
表 1
id | 外键 | 场 |
---|---|---|
1 | 7 | 一些价值 |
2 | 8 | 一些价值 |
3 | 8 | 一些价值 |
… | … | … |
表 2
id | 场 |
---|---|
… | … |
7 | 一些价值 |
8 | 一些价值 |
… | … |
我需要从TABLE 2字段(一些值)中获取数据类型