如何在PostgreSQL中使用单个查询更新两列

时间:2016-07-20 20:12:56

标签: sql postgresql

我需要在单个表( v_default_settings )中更新两列( default_setting_value default_setting_enabled )的值,具体取决于同一个表中的另一列( default_setting_subcategory )。我还在学习PostgreSQL,但是,在我的研究中,这些是我提出的两个UPDATE语句。我已经针对数据库测试了它们,它们可以单独工作。 我更愿意在一个查询中执行此操作 - 那么我该怎么办呢?

SQL QUERY#1

<?php

$person = new People;
echo $person->getPersonInfo();

class People
{
private $person_info = $this->getName . ' ' . $this->getGender . ' and ' . $this->getAge() . '.';
private $person = array(
'name' => 'Sarah',
'gender' => 'female',
'age' => 21
);

public function __construct()
{
    echo 'Class is created.' . '<br>';
}

public function getName()
{
    return $this->person['name'];
}

public function getGender()
{
    return $this->person['gender'];
}

public function getAge()
{
    return $this->person['age'];
}

public function getPersonInfo()
{
    return $this->person_info;
}
}

SQL QUERY#2:

UPDATE v_default_settings
SET default_setting_value = CASE
WHEN default_setting_subcategory = 'base' THEN '/usr'
WHEN default_setting_subcategory = 'bin' THEN ''
WHEN default_setting_subcategory = 'call_center' THEN '/etc/freeswitch/autoload_configs'
WHEN default_setting_subcategory = 'conf' THEN '/etc/freeswitch'
WHEN default_setting_subcategory = 'db' THEN '/var/lib/freeswitch/db'
WHEN default_setting_subcategory = 'dialplan' THEN '/etc/freeswitch/dialplan'
WHEN default_setting_subcategory = 'extensions' THEN '/etc/freeswitch/directory'
WHEN default_setting_subcategory = 'grammar' THEN '/usr/share/freeswitch/grammar'
WHEN default_setting_subcategory = 'log' THEN '/var/log/freeswitch'
WHEN default_setting_subcategory = 'mod' THEN '/usr/lib/freeswitch/mod'
WHEN default_setting_subcategory = 'phrases' THEN '/etc/freeswitch/lang'
WHEN default_setting_subcategory = 'recordings' THEN '/var/lib/freeswitch/recordings'
WHEN default_setting_subcategory = 'scripts' THEN '/usr/share/freeswitch/scripts'
WHEN default_setting_subcategory = 'sip_profiles' THEN '/etc/freeswitch/sip_profiles'
WHEN default_setting_subcategory = 'sounds' THEN '/usr/share/freeswitch/sounds'
WHEN default_setting_subcategory = 'storage' THEN '/var/lib/freeswitch/storage'
WHEN default_setting_subcategory = 'voicemail' THEN '/var/lib/freeswitch/storage/voicemail'
END
WHERE default_setting_subcategory IN ('base','bin','call_center','conf','db','dialplan','extensions','grammar','log','mod','phrases','recordings','scripts','sip_profiles','sounds','storage','voicemail');

3 个答案:

答案 0 :(得分:2)

使用array subscriptions会更清晰

update v_default_settings
set
    default_setting_value =
        (array['/usr','','/etc/freeswitch/autoload_configs',...])[
            array_position (
                array['base','bin','call_center',...],
                default_setting_subcategory
            )
        ],
    default_setting_enabled = ...
where default_setting_subcategory in ('base','bin','call_center',...)

如果您想要case,请使用更简单的语法:

update v_default_settings
set
    default_setting_value = case default_setting_subcategory
        when 'base' then '/usr'
        when 'bin' then ''
        when 'call_center' then '/etc/freeswitch/autoload_configs'
        when 'conf' then '/etc/freeswitch'
        when 'db' then '/var/lib/freeswitch/db'
        ...
    end,
    default_setting_enabled = case
        when default_setting_subcategory in ('base','bin','conf',...) then true
        else false
    end
    ...
where ...

使用in运算符

可以简化第二种情况

答案 1 :(得分:1)

您只需将其他列添加到SET语句中:

UPDATE v_default_settings
SET default_setting_value = CASE
WHEN default_setting_subcategory = 'base' THEN '/usr'
WHEN default_setting_subcategory = 'bin' THEN ''
WHEN default_setting_subcategory = 'call_center' THEN '/etc/freeswitch/autoload_configs'
WHEN default_setting_subcategory = 'conf' THEN '/etc/freeswitch'
WHEN default_setting_subcategory = 'db' THEN '/var/lib/freeswitch/db'
WHEN default_setting_subcategory = 'dialplan' THEN '/etc/freeswitch/dialplan'
WHEN default_setting_subcategory = 'extensions' THEN '/etc/freeswitch/directory'
WHEN default_setting_subcategory = 'grammar' THEN '/usr/share/freeswitch/grammar'
WHEN default_setting_subcategory = 'log' THEN '/var/log/freeswitch'
WHEN default_setting_subcategory = 'mod' THEN '/usr/lib/freeswitch/mod'
WHEN default_setting_subcategory = 'phrases' THEN '/etc/freeswitch/lang'
WHEN default_setting_subcategory = 'recordings' THEN '/var/lib/freeswitch/recordings'
WHEN default_setting_subcategory = 'scripts' THEN '/usr/share/freeswitch/scripts'
WHEN default_setting_subcategory = 'sip_profiles' THEN '/etc/freeswitch/sip_profiles'
WHEN default_setting_subcategory = 'sounds' THEN '/usr/share/freeswitch/sounds'
WHEN default_setting_subcategory = 'storage' THEN '/var/lib/freeswitch/storage'
WHEN default_setting_subcategory = 'voicemail' THEN '/var/lib/freeswitch/storage/voicemail'
END,
    default_setting_enabled = CASE
WHEN default_setting_subcategory = 'base' THEN 'true'
WHEN default_setting_subcategory = 'bin' THEN 'true'
WHEN default_setting_subcategory = 'call_center' THEN 'false'
WHEN default_setting_subcategory = 'conf' THEN 'true'
WHEN default_setting_subcategory = 'db' THEN 'true'
WHEN default_setting_subcategory = 'dialplan' THEN 'false'
WHEN default_setting_subcategory = 'extensions' THEN 'false'
WHEN default_setting_subcategory = 'grammar' THEN 'true'
WHEN default_setting_subcategory = 'log' THEN 'true'
WHEN default_setting_subcategory = 'mod' THEN 'true'
WHEN default_setting_subcategory = 'phrases' THEN 'false'
WHEN default_setting_subcategory = 'recordings' THEN 'true'
WHEN default_setting_subcategory = 'scripts' THEN 'true'
WHEN default_setting_subcategory = 'sip_profiles' THEN 'false'
WHEN default_setting_subcategory = 'sounds' THEN 'true'
WHEN default_setting_subcategory = 'storage' THEN 'true'
WHEN default_setting_subcategory = 'voicemail' THEN 'true'
END
WHERE default_setting_subcategory IN ('base','bin','call_center','conf','db','dialplan','extensions','grammar','log','mod','phrases','recordings','scripts','sip_profiles','sounds','storage','voicemail');

答案 2 :(得分:0)

这就是我最终的目标(至少目前我能理解)。发布它以防其他人发现它有用:

UPDATE v_default_settings
        SET default_setting_value = v.value,
            default_setting_enabled = v.enabled
        FROM (VALUES
                  ('base',          '/usr',                                        'true' ),
                  ('bin',           '',                                            'true' ),
                  ('call_center',   '/etc/freeswitch/autoload_configs',            'false'),
                  ('conf',          '/etc/freeswitch',                             'true' ),
                  ('db',            '/var/lib/freeswitch/db',                      'true' ),
                  ('dialplan',      '/etc/freeswitch/dialplan',                    'false'),
                  ('extensions',    '/etc/freeswitch/directory',                   'false'),
                  ('grammar',       '/usr/share/freeswitch/grammar',               'true' ),
                  ('log',           '/var/log/freeswitch',                         'true' ),
                  ('mod',           '/usr/lib/freeswitch/mod',                     'true' ),
                  ('phrases',       '/etc/freeswitch/lang',                        'false'),
                  ('recordings',    '/var/lib/freeswitch/recordings',              'true' ),
                  ('scripts',       '/usr/share/freeswitch/scripts',               'true' ),
                  ('sip_profiles',  '/etc/freeswitch/sip_profiles',                'false'),
                  ('sounds',        '/usr/share/freeswitch/sounds',                'true' ),
                  ('storage',       '/var/lib/freeswitch/storage',                 'true' ),
                  ('voicemail',     '/var/lib/freeswitch/storage/voicemail',       'true' )
              ) AS v(subcategory,value,enabled)
        WHERE v.subcategory = v_default_settings.default_setting_subcategory;

我仍然非常感谢@siyual和@ clodoaldo-neto协助以前的答案。