2017-02-22 1 views
0

단일 쿼리로 정보를 업데이트하고 추가해야합니다.하나의 쿼리로 삽입 및 업데이트

경쟁 현재 내가

INSERT INTO `competitor`(`ID`, `Name`, `Age`, `DiveNr`, `Difficulty`) 
VALUES(@idSql,@NameSql,@AgeSql,@DiveNrSql,@DiffSql) 

를 사용하려고했습니다 그러나 빨리이 불가능 것을 깨달았다

ID Name Age  DiveNr   Difficulty 
|----|-------|----|------------------|-----------| 
| 1 | Test | 22 | 000110,011111 | 3,5  | 
|____|_______|____|__________________|___________| 

- 테이블. 이 같은

ID Name Age  DiveNr    Difficulty 
|----|-------|----|----------------------|-----------| 
| 1 | Test | 22 | 000110,011111,230400 | 3,5,6  | 
|____|_______|____|______________________|___________| 

뭔가 : 내가 원하는 무엇

특정 ID에 DiveNrDifficulty에 더 많은 항목을 추가 할 수있다?

SELECT ID FROM competitor WHERE ID=1; 
INSERT INTO competitor(DiveNr,Difficulty)VALUES(@DiveNrSql,@DiffSql) 

어떻게하면됩니까?

+6

데이터 구조를 수정하십시오. ID 목록을 쉼표로 구분 된 목록에 저장하는 것은 SQL을 사용하여 데이터를 저장하는 방법이 아닙니다. –

+0

@GordonLinoff 실제로는 아니지만 C#의 방법입니다. – Joel

+0

내 책에서 원하는 것은 레코드를 업데이트하는 것입니다. 삽입하려는 이유는 무엇입니까? – Pikoh

답변

0

먼저 해당 컬럼의 이전 값을 얻을 : -

declare @oldDiveNrSql varchar(100) 
declare @oldDiffSql varchar(100) 

set @oldDiveNrSql = (select DiveNr from competitor where [email protected]) 
set @oldDiffSql = (select Difficulty from competitor where [email protected]) 

다음 새 값으로 이전 값을 추가 : -

declare @newDiveNrSql varchar(100) 
declare @newDiffSql varchar(100) 

set @newDiveNrSql [email protected]+','[email protected] 
set @newDiffSql [email protected]','[email protected] 

마침내는 새 값으로 행을 업데이트 : -

update competitor set [email protected],[email protected] 
where [email protected] 
관련 문제