2014-02-09 34 views
-1

當我嘗試插入下面的值時,出現錯誤消息「#1136 - 列計數與第1行的值計數不匹配」。有沒有辦法讓更短的數據行在最後自動插入爲空,而無需手動進入每行代碼?SQL INSERT - 如果列數不匹配,我可以插入空值嗎?

VALUES ('','BUF','','QB1','','QB BILLS','Face=0x52','#0','25','69','13','13','56','81','81','81','3','12','3'); 
VALUES ('','BUF','','QB2','frank REICH','Face=0x22','#14','25','69','13','13','31','44','25','50','3','3','2'); 
VALUES ('','BUF','','RB1','thurman THOMAS','Face=0x83','#34','38','69','63','25','75','50','10','7','8','8'); 
VALUES ('','BUF','','RB2','jamie MUELLER','Face=0x51','#41','44','69','25','88','50','25','5','3','6','2'); 
VALUES ('','BUF','','RB3','kenneth DAVIS','Face=0xa5','#23','38','69','25','19','50','31','5','2','7','2'); 
VALUES ('','BUF','','RB4','don SMITH','Face=0x8b','#30','38','69','25','19','50','31','3','3','8','2'); 
VALUES ('','BUF','','WR1','james LOFTON','Face=0x81','#80','25','69','38','13','50','56','1','8','13','6'); 
VALUES ('','BUF','','WR2','andre REED','Face=0xb6','#83','25','69','56','13','56','69','1','9','10','10'); 
VALUES ('','BUF','','WR3','don BEEBE','Face=0x40','#82','25','69','44','13','50','44','1','4','13','2'); 
VALUES ('','BUF','','WR4','al EDWARDS','Face=0x9a','#85','25','69','19','13','50','44','1','4','7','2'); 
VALUES ('','BUF','','TE1','keith MCKELLER','Face=0xb7','#84','25','69','38','50','50','50','1','6','7','6'); 
VALUES ('','BUF','','TE2','pete METZELAARS','Face=0x50','#88','25','69','19','44','50','31','1','3','5','2'); 
VALUES ('','BUF','','C','kent HULL','Face=0x1e','#67','25','69','38','69'); 
VALUES ('','BUF','','LG','jim RITCHER','Face=0x7','#51','25','69','38','56'); 
VALUES ('','BUF','','RG','john DAVIS','Face=0x24','#65','25','69','25','63'); 
VALUES ('','BUF','','LT','will WOLFORD','Face=0x48','#69','25','69','25','50'); 
VALUES ('','BUF','','RT','howard BALLARD','Face=0x88','#75','25','69','19','63'); 
VALUES ('','BUF','','RE','bruce SMITH','Face=0x88','#78','44','56','69','75','25','81','116','7'); 
VALUES ('','BUF','','NT','jeff WRIGHT','Face=0xb','#91','25','31','31','50','19','19','30','7'); 
VALUES ('','BUF','','LE','leon SEALS','Face=0xac','#96','25','31','38','44','31','50','25','18'); 
VALUES ('','BUF','','ROLB','darryl TALLEY','Face=0xad','#56','31','44','50','38','44','63','25','25'); 
VALUES ('','BUF','','RILB','ray BENTLEY','Face=0x30','#50','25','31','38','38','31','56','13','10'); 
VALUES ('','BUF','','LILB','shane CONLAN','Face=0x2f','#58','31','44','50','56','19','69','13','13'); 
VALUES ('','BUF','','LOLB','c. BENNETT','Face=0x82','#97','38','50','63','63','19','69','29','7'); 
VALUES ('','BUF','','RCB','nate ODOMES','Face=0xc3','#37','38','44','56','38','38','56','0','25'); 
VALUES ('','BUF','','LCB','kirby JACKSON','Face=0x89','#47','25','31','44','38','50','50','0','64'); 
VALUES ('','BUF','','FS','mark KELSO','Face=0x26','#38','31','38','50','38','44','44','2','39'); 
VALUES ('','BUF','','SS','leonard SMITH','Face=0x84','#46','31','38','50','44','44','50','2','40'); 
VALUES ('','BUF','','K','scott NORWOOD','Face=0x29','#11','56','81','81','31','44','44','6'); 
VALUES ('','BUF','','P','rick TUTEN','Face=0x20','#10','25','56','44','31','19','63','3'); 
+1

不,不是自動。你如何生成這些SQL語句? –

+0

這些列在'INSERT INTO table_name(column1,column2)'中。他們需要匹配,因爲MySQL如何知道哪些條目可以更改爲空 –

+2

爲什麼我懷疑可變數量的列應該真正作爲單獨的行存儲在關聯/結點表中? –

回答

1

從你的MySQL聲明 刪除不需要的列例如

mysqli_query($con,"INSERT INTO files (1, 2, unwanted, 4) 
VALUES ('$01', '$02', '' ,'$04')"); 

使其

mysqli_query($con,"INSERT INTO files (1, 2, 4) 
    VALUES ('$01', '$02' ,'$04')"); 

或加入一個變量,它爲空,

<?php 
    $null = ''; 
VALUES ('$null','BUF','$null','ROLB','darryl TALLEY','Face=0xad','#56','31','44','50','38','44','63','25','25'); 
     ?> 
+0

您是否同意您需要手動執行此操作,因此不回答該問題? –

+0

您認爲TO使用PHP。 –

+0

大聲笑,我以爲他使用php –

0

我哈我已根據您當前的職位做出以下假設。我假設你想把足球運動員的分數加入到每個球員的固定數據表中。我不推薦這樣做,因爲每次你想添加分數時,你都會繼續添加一個新的

我已經爲你製作瞭如下的例子,你可以製作一個分數表和玩家表,它將從分數中分離固定數據,如玩家姓名,圖片等。

試試這個:

SQL查詢:

SELECT players.name, group_concat(score.score) 
FROM players 
JOIN score 
ON players.id = score.id_players 
GROUP BY players.id; 


CREATE TABLE players (
    id int auto_increment primary key, 
    name varchar(30) 
); 

CREATE TABLE score (
    id int auto_increment primary key, 
    score int, 
    id_players int 
); 

INSERT INTO players (name) 
VALUES ('QB BILLS'), ('frank REICH'); 


INSERT INTO score (id_players, score) 
VALUES (1,25),(1,69), (1,13),(1,13),(1,56),(1,81),(1,81), 
(1,81),(1,3),(1,12),(1,3), (2,25),(2,69),(2,13),(2,13),(2,31),(2,44),(2,25),(2,50),(2,3),(2,3),(2,2); 

SQLFiddle demo

+0

謝謝,但那不是我想要做的。 – tecmosuperbowl

+0

@tecmosuperbowl你能更詳細地解釋你正在尋找什麼。你以什麼形式獲取數據? –

相關問題