Is there a way to combine these?

Thalassicus

Bytes and Nibblers
Joined
Nov 9, 2005
Messages
11,057
Location
Texas
Is there a way to combine these into one statement? Basically I just want to select all BuildingType from 4 different tables.

Code:
INSERT INTO Policy_BuildingClassConstructionModifiers
	(PolicyType, BuildingClassType, YieldMod)
SELECT 'POLICY_PROFESSIONAL_ARMY', (SELECT BuildingType FROM Building_DomainFreeExperiences), '25';

INSERT INTO Policy_BuildingClassConstructionModifiers
	(PolicyType, BuildingClassType, YieldMod)
SELECT 'POLICY_PROFESSIONAL_ARMY', (SELECT BuildingType FROM Building_UnitCombatFreeExperiences), '25';

INSERT INTO Policy_BuildingClassConstructionModifiers
	(PolicyType, BuildingClassType, YieldMod)
SELECT 'POLICY_PROFESSIONAL_ARMY', (SELECT BuildingType FROM Building_DomainProductionModifiers), '25';

INSERT INTO Policy_BuildingClassConstructionModifiers
	(PolicyType, BuildingClassType, YieldMod)
SELECT 'POLICY_PROFESSIONAL_ARMY', (SELECT BuildingType FROM Building_UnitCombatProductionModifiers), '25';
 
There is but it isn't very different from what you have done. You use the UNION operator on your select statements as per the following...

Code:
INSERT INTO Policy_BuildingClassConstructionModifiers(PolicyType, BuildingClassType, YieldMod)
SELECT 'POLICY_PROFESSIONAL_ARMY', BuildingType, '25' FROM (
  SELECT BuildingType FROM Building_DomainFreeExperiences
UNION
  SELECT BuildingType FROM Building_UnitCombatFreeExperiences
UNION
  SELECT BuildingType FROM Building_BuildingClassConstructionModifiers
UNION
  SELECT BuildingType FROM Building_UnitCombatProductionModifiers
);

This will also avoid duplicate entries if the same building type appears in multiple source tables, if you need the duplicates use 'UNION ALL'
 
Thanks! I thought it was something like that but I was mistakenly looking up the join operator. :crazyeye:

After working with this a bit I realized I had several errors. The part that confused me in particular is how the "from where" clause is not included with the "type" parameter, but after it. I've posted it below as reference for anyone seeking to do similar things in the future:

Code:
INSERT INTO Policy_BuildingClassConstructionModifiers(
    PolicyType, 
    BuildingClassType, 
    YieldMod)
SELECT 
    'POLICY_PROFESSIONAL_ARMY', 
    Type, 
    25 
FROM Buildings WHERE (
    Defense > 0
    OR GlobalDefenseMod > 0 
    OR Experience > 0
    OR GlobalExperience > 0
    OR Type IN (SELECT BuildingType FROM Building_DomainFreeExperiences)
    OR Type IN (SELECT BuildingType FROM Building_UnitCombatFreeExperiences)
    OR Type IN (SELECT BuildingType FROM Building_DomainProductionModifiers)
    OR Type IN (SELECT BuildingType FROM Building_UnitCombatProductionModifiers)
);

The reason I went with "or" clauses is faster execution from short-circuit evaluation. I know it's insignificant for a database transaction this small, but efficiency is a good habit. :)
 
Back
Top Bottom