SQL: INSERT INTO … ЗНАЧЕНИЯ.. ВЫБРАТЬ

Сначала давайте добавим типы в класс Mammal.

interface AnimalProperties {
  legs: number;
  wings: boolean;
}

class Animal {
  constructor(customProperties: T) {
    this.properties = { legs: 4, wings: false, ...customProperties };
  }

  public properties: AnimalProperties & T;
}

Используя универсальный класс, мы можем позволить животному иметь свойства, которые мы не знаем заранее. Пример:

const { legs, wings, predator } = new Animal({ predator: false }).properties;

Теперь мы можем перейти к специализированному классу Kangaroo. Он представляет собой смесь Animal и некоторых дополнительных свойств. Давайте назовем их KangarooProperties.

Примечание: переопределение AnimalProperties должно быть возможным, но не обязательно. Вот почему я расширяю Partial.

interface KangarooProperties extends Partial {
  mammal: true;
}

class Kangaroo extends Animal {
  constructor(customProperties: T) {
    super({ legs: 2, mammal: true, ...customProperties })
  }
}

Наш финал properties правильно распознан.

const { cute, legs, mammal, wings } = new Kangaroo({ cute: true }).properties;

Если ваш Kangaroo не должен сам принимать какие-либо опции, вы можете пропустить общую часть.

interface KangarooProperties extends Partial {
  mammal: true;
}

class Kangaroo extends Animal {
  constructor() {
    super({ legs: 2, mammal: true })
  }
}

См. Игровая площадка TypeScript .

23
задан marc_s 31 July 2009 в 16:35
поделиться

4 ответа

Insert into TableA (ColA, ColB, ColC) . . .

Must be the column names as the are in Table A. В

Insert into TableA (ColA, ColB, ColC) . . .
Select TableX.Col1, TableY.Col1, TableZ.Col5 
From TableX, TableY, TableZ
Where . . . 
4
ответ дан 29 November 2019 в 01:02
поделиться
INSERT INTO TableA(colA, colB, colC)
  SELECT TableX.valA, TableY.valB, TableZ.valC
    FROM TableX
   INNER JOIN TableY ON :......
   INNER JOIN TableZ ON ........

Of course, TableX, TableY and TAbleZ might also be related in some other way (not INNER JOIN).

If you cannot find any relation between the tables AT ALL, you could also do three separate

SELECT @value1 = valA FROM TableX WHERE ......
SELECT @value2 = valB FROM TableY WHERE ......
SELECT @value3 = valC FROM TableZ WHERE ......

and then an insert like this:

INSERT INTO TableA(colA, colB, colC)
             VALUES(@value1, @value2, @value3)

That's the ultimate last resort, you can can't express everything in a single SELECT statement.

Marc

39
ответ дан 29 November 2019 в 01:02
поделиться

В ответ на ответ marc_s вы можете запросить из несвязанных таблиц в as select, например:

INSERT INTO TableA
    (colA, colB, colC)
SELECT
    (SELECT valA FROM TableX WHERE ...),
    (SELECT valB FROM TableY WHERE ...),
    (SELECT valC FROM TableZ WHERE ...)
24
ответ дан 29 November 2019 в 01:02
поделиться

You will need to join the tables that you want to make the selection from.

Here is a resource on SQL joins:

www.w3schools.com/sql/sql_join.asp

You also might want to check out this free PDF book from the guys at www.simple-talk.com that covers SQL basics:

SQL Server Crib Sheet Compendium

0
ответ дан 29 November 2019 в 01:02
поделиться
Другие вопросы по тегам:

Похожие вопросы: