programing

다른 선택 쿼리의 결과가 포함된 테이블에 데이터 삽입

iphone6s 2023. 9. 4. 19:38
반응형

다른 선택 쿼리의 결과가 포함된 테이블에 데이터 삽입

다음 문제에 대해 도움을 요청합니다.테이블이 두 개 있습니다.Table_1열은itemid,locationid,quantity

Table_2열은itemid,location1,location2,location3

데이터를 복사하고 싶습니다.Table_1(만)quantity열)로Table_2(속으로)location1컬럼).itemid두 테이블 모두 동일합니다(Table_1항목 ID가 중복됨)이 있기 때문에 새 테이블에 복사하고 각 위치를 열로 하여 모든 수량을 하나의 행에 보관하려는 이유입니다.아래 쿼리를 사용하고 있는데 작동하지 않습니다.

INSERT INTO 
Table_2(location1) 
(
 SELECT qty 
 FROM Table_1 
 WHERE locationid = 1 AND Table_1.locationid = Table_2.locationid
)

한다면table_2이 비어 있습니다. 그런 다음 다음 문장 삽입을 시도해 보십시오.

insert into table_2 (itemid,location1) 
select itemid,quantity from table_1 where locationid=1

한다면table_2이미 포함되어 있습니다.itemid값을 입력한 다음 다음 업데이트 문을 사용하십시오.

update table_2 set location1=
(select quantity from table_1 where locationid=1 and table_1.itemid = table_2.itemid)
INSERT INTO `test`.`product` ( `p1`, `p2`, `p3`) 
SELECT sum(p1), sum(p2), sum(p3) 
FROM `test`.`product`;

다음은 이러한 쿼리의 예입니다.

INSERT INTO [93275].[93276].[93277].[93278] ( [Mobile Number], [Mobile Series], [Full Name], [Full Address], [Active Date], company ) IN 'I:\For Test\90-Mobile Series.accdb
SELECT [1].[Mobile Number], [1].[Mobile Series], [1].[Full Name], [1].[Full Address], [1].[Active Date], [1].[Company Name]
FROM 1
WHERE ((([1].[Mobile Series])="93275" Or ([1].[Mobile Series])="93276")) OR ((([1].[Mobile Series])="93277"));OR ((([1].[Mobile Series])="93278"));

언급URL : https://stackoverflow.com/questions/6354132/insert-data-into-table-with-result-from-another-select-query

반응형