如何减少查询次数

时间:2015-04-13 05:58:57

标签: mysql database

我正在尝试生成一个catalog表,这样每次我添加到其他2个表时它都是相同的id。目前,我必须运行多个查询来生成。只是想尝试简化它。这是我的原始查询,任何合并的想法都值得赞赏!

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = '32g bins')
where item_name = '32g bins';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Collection Bags')
where item_name = 'Collection Bags';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Chicken Wire Bins')
where item_name = 'Chicken Wire Bins';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = '5 gallon buckets')
where item_name = '5 gallon buckets';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Spray Paint')
where item_name = 'Spray Paint';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Open Sign')
where item_name = 'Open Sign';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Pricing Sign')
where item_name = 'Pricing Sign';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Calrecycle Sign')
where item_name = 'Calrecycle Sign';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Water Dispenser')
where item_name = 'Water Dispenser';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Water Bottles')
where item_name = 'Water Bottles';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Safe')
where item_name = 'Safe';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Security System')
where item_name = 'Security System';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Hand Sanitizer')
where item_name = 'Hand Sanitizer';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Working Gloves')
where item_name = 'Working Gloves';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Cleaning Gloves')
where item_name = 'Cleaning Gloves';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Ear Plugs')
where item_name = 'Ear Plugs';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Safety Goggles')
where item_name = 'Safety Goggles';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Office Desk')
where item_name = 'Office Desk';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Change/Cash Deposit Bags')
where item_name = 'Change/Cash Deposit Bags';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Laptop')
where item_name = 'Laptop';

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog where item_name = 'Writing Pens')
where item_name = 'Writing Pens'

;

3 个答案:

答案 0 :(得分:3)

尝试这样的事情:

update equipment_needed as t1
set  `catalog_id` = (select `id`
    from equipment_catalog as t2
    where t2.item_name = t1.item_name)

答案 1 :(得分:1)

这似乎不是SQL Server的问题,但我认为它在MySQL中也是如此:

update equipment_needed
set  `catalog_id` = (select `id` from equipment_catalog 
where item_name = equipment_needed.item_name)

如果您需要限制提取的名称,最后可以添加更多条件,如下所示:

...
where item_name = equipment_needed.item_name)
item_name in ('Calrecycle Sign', 'Water Dispenser', ...);

答案 2 :(得分:0)

更新equipment_needed为t1 set item_name =(select item_name 从equipment_catalog为t2 WHERE22.id = t1.needed_id) ; 更新equipment_needed为t1 set quantity_type =(选择quantity_type 从equipment_catalog为t2 WHERE22.id = t1.needed_id) ; 更新equipment_needed为t1 设置category =(选择category 来自equipment_catalog为t2 其中t2.id = t1.needed_id) ; #run for equipment_needed

将equipment_owned更新为t1 set item_name =(select item_name 从equipment_catalog为t2 WHERE t2.id = t1.owned_id) ; 将equipment_owned更新为t1 set quantity_type =(选择quantity_type 从equipment_catalog为t2 WHERE t2.id = t1.owned_id) ; 将equipment_owned更新为t1 设置category =(选择category 来自equipment_catalog为t2 其中t2.id = t1.owned_id) ; #run for equipment_owned

翻转它以使用ID进行验证而不是名称,因为它更简单。