简体   繁体   English

SQL联合与具有不同类型的表

[英]SQL Union with tables with different types

I have the following tables, in which I store the properties of my application (FYI I'm not directly using SQL in my app but Python Django which allows me to have a structure like that): 我有以下表格,其中我存储了我的应用程序的属性(仅供参考我不是直接在我的应用程序中使用SQL而是Python Django,它允许我有这样的结构):

      string_properties
 id  |  name         | value (type=string)
  1  :  module.ip    : "127.0.0.1"
  2  :  module.name  : "a_name"
     :               :

      integer_properties
 id  |  name       | value (type=integer)
  1  : module.size : 10
  2  : module.port : 80
     :             :

      boolean_properties
 id  |  name             | value (type=bool)
  1  : module.activated  : 1
     :                   :

And I would like to generate a view which shows for each property, the value associated. 我想生成一个视图,显示每个属性,相关的值。

I tried several strategies to do so: 我尝试了几种策略:

1- using a basic UNION: it didn't work as the "value" column isn't the same type in all my tables 1-使用基本的UNION:它不起作用,因为“value”列在我的所有表中都不是同一类型

2- changing the names of the "value" column, adding a suffix to it (like value_int, value_string, etc). 2-更改“value”列的名称,为其添加后缀(如value_int,value_string等)。 Then I would have expected to be able to use an UNION statement but it failed too. 然后我本来希望能够使用UNION语句,但它也失败了。 I would be happy with a result table like below: 我很满意如下的结果表:

      result
 id  |  name             | value_int | value_string | value_bool
  1  :  module.ip        :     null  : "127.0.0.1"  : null
  2  :  module.name      :     nulll : "a_name"     : null
  3  : module.activated  :     null  :  null        :  1
  4  : module.size       :      10   :  null        : null
  5  : module.port       :      80   :  null        : null
     :                   :

3- I guess I could convert all my values to string and then UNION the table, but it's ugly to lose the types. 3-我想我可以将所有值转换为字符串,然后将UNION转换为表格,但丢失类型会很难看。

What do you think? 你怎么看?

SELECT id, name, value AS value_int, null AS value_string, null AS value_bool
FROM integer_properties
UNION ALL
SELECT id, name, null AS value_int, value AS value_string, null AS value_bool
FROM string_properties
UNION ALL
SELECT id, name, null AS value_int, null AS value_string, value AS value_bool
FROM boolean_properties

I'd suggest using a UNION ALL as you suggest, and casting the values. 我建议你按照你的建议使用UNION ALL并投射值。

SELECT 
    id, 
    name, 
    value
FROM
    string_properties
UNION ALL 
SELECT 
    id, 
    name, 
    CAST(value AS CHAR(50)) AS value
FROM
    integer_properties
UNION ALL 
SELECT 
    id, 
    name, 
    CAST(value AS CHAR(50)) AS value
FROM
    boolean_properties

If you want to know where a value came from you could add a fourth column - eg SELECT "string_properties" AS parent_table among the other fields from that table (and do the same for the other tables, with a different string value, also calling it parent_table ) 如果你想知道一个值来自何处你可以添加第四列 - 例如SELECT "string_properties" AS parent_table在该表的其他字段中(并对其他表执行相同的操作,使用不同的字符串值,也调用它parent_table

If you "don't want to lose the types", you can select the underlying type as a constant in the UNION : 如果您“不想丢失类型”,则可以在UNION选择基础类型作为常量:

CREATE VIEW all_properties AS
SELECT id, name, typ, value
FROM    (
        SELECT sp.id AS id
        , sp.name AS name
        , 'String' AS typ
        , sp.value AS value
        FROM string_properties sp
UNION ALL
        SELECT ip.id AS id
        , ip.name AS name
        , 'Integer' AS typ
        , ip.value::varchar AS value
        FROM integer_properties ip
UNION ALL
        SELECT bp.id AS id
        , bp.name AS name
        , 'Boolean' AS typ
        , bp.value::varchar AS value
        FROM boolean_properties bp
        ) x
        ;
SELECT * FROM all_properties ap
ORDER BY id,name
        ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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