programing

MySQL 삽입 위치...값 및 선택

luckcodes 2022. 9. 12. 19:53

MySQL 삽입 위치...값 및 선택

Select-Query에서 얻은 사전 설정 값과 값을 삽입하는 방법이 있습니까?예를 들어 다음과 같습니다.

INSERT INTO table1 VALUES ("A string", 5, [int]).

값 "A string"과 숫자 5가 있는데 다음과 같은 선택 항목에서 [int] 값을 찾아야 합니다.

SELECT idTable2
FROM table2
WHERE ...

테이블 1에 넣을 아이디를 얻을 수 있습니다.

이것을 하나의 스테이트먼트로 통합하는 방법

를 사용합니다.insert ... selectquery 및 기존 값을 에 저장합니다.select:

insert into table1
select 'A string', 5, idTable2
from table2
where ...

다음과 같은 서브쿼리를 사용합니다.

INSERT INTO table1 VALUES ("A string", 5, (SELECT ...)).
 
table_name1에 삽입(ID,이름.주소.contact_number)ID, 이름, 주소, 연락처_번호 FROM table_name2를 선택합니다.

이거 먹어봐

INSERT INTO TABLE1 (COL1 , COL2,COL3) values
('A STRING' , 5 , (select idTable2 from Table2) )
where ...

다른 모든 답변은 문제를 해결하고 내 답변은 다른 답변과 동일하게 작동하지만, 좀 더 알기 쉬운 방식으로 작동합니다(MySQL에서는... 다른 SQL 서버에서는 작동하지 않습니다).

INSERT INTO table1 SET 
  stringColumn  = 'A String', 
  numericColumn = 5, 
  selectColumn  = (SELECT idTable2 FROM table2 WHERE ...);

MySQL 문서를 참조할 수 있습니다.INSERT 구문

INSERT INTO table1 
SELECT "A string", 5, idTable2
FROM table2
WHERE ...

참조: http://dev.mysql.com/doc/refman/5.6/en/insert-select.html

INSERT INTO table1(Table2Id, KeyTypeEnumId, SortOrder, TenantId)
    SELECT Id, 1, 1, TenantId
    FROM table2
    WHERE WebsitePageTypeEnumId = 10 AND ElementTypeEnumId = 16
INSERT INTO table1 (col1, col2)
SELECT "a string", 5, TheNameOfTheFieldInTable2
FROM table2 where ...

다음을 시도해 보십시오.

INSERT INTO table1 
SELECT 'A string', 5, idTable2 idTable2 FROM table2 WHERE ...

이것을 시험해 보세요.

INSERT INTO table1 SELECT "A string", 5, idTable2 FROM table2 WHERE ...

언급URL : https://stackoverflow.com/questions/15523597/mysql-insert-into-values-and-select