如何在插入中包含select语句的值? (PostgreSQL的)

时间:2015-06-30 14:34:03

标签: postgresql

我有这个选择声明:

select it.item_id
from item as it
where it.owning_collection in (select col.collection_id
from collection as col
where col.name like '%Restricted%'

返回3k左右的结果。现在我想在另一个表中插入每个结果,并将item_id作为以下参数之一:

insert into metadatavalue (metadata_value_id, **item_id**, metadata_field_id, text_value, text_lang, place, confidence)

但由于我对数据库不是很熟悉,所以我不确定如何制作这些多个插页

insert语句中所需的所有其他信息都是固定值。

表格结构:

Table Item
*item_id
*submitter_id
*in_archive
*withdrawn
*last_modified
*owning_collection
*dicoverable

Table metadata
*metadata_value_id
*item_id
*metadata_field_id
*text_value
*text_lang
*place
*authority
*confidence

2 个答案:

答案 0 :(得分:1)

insert into metadatavalue (metadata_value_id, item_id, metadata_field_id, text_value, text_lang, place, confidence)
select 'metadata_value_id',it.item_id,'metadata_field_id','text_value', 'text_lang', 'place', 'confidence'
from item it
where it.owning_collection in (select col.collection_id
from collection as col
where col.name like '%Restricted%')

替换'撇号'列的默认值。

Further reading

答案 1 :(得分:1)

<?php for ($x = 1; $x <= 6; $x++): ?>
  <button type="button" data-index="<?= $x ?>">
    <span><?= $x ?></span>
  </button>
<?php endfor; ?>
<?php for ($x = 1; $x <= 6; $x++): ?>
  <div class="some_div" id="someDiv<?= $x ?>"></div>
<?php endfor; ?>

<script type="text/javascript">
  $('button').click(function() {
    var selected = !$(this).data('selected');
    $(this).data('selected', selected);

    var index = $(this).data('index');
    $('#someDiv' + index).toggle(selected);
  });
</script>

使用常量值替换INSERT INTO metadatavalue (item_id, metadata_field_id, text_value, text_lang, place, confidence) SELECT it.item_id, <c1>, <c2>, <c3>, <c4>, <c5> FROM item AS it JOIN collection AS col ON col.collection_id = it.owning_collection WHERE col.name LIKE '%Restricted%' 等。另请注意,我已将您的<c1>查询重写为更高效的SELECT