[英]How to INSERT data into a specific column without NULLs in the other columns?
I have a table ("table1") with 3 columns called col1, col2 and col3 (each is VARCHAR) with 4 values as shown below:我有一个表(“table1”),其中包含 3 个名为 col1、col2 和 col3(每个都是 VARCHAR)的列,其中包含 4 个值,如下所示:
col1 col2 col3
datA1 datB1 datC1
datA2
I need an ability to add a data at any time into any column NOT affecting on the other ones.我需要能够随时将数据添加到不影响其他列的任何列中。 The very popular code in the Internet is that (say, we need to add data only to the columns col2 and col3):
网上很流行的代码是这样的(比如说,我们只需要在col2和col3列中添加数据):
INSERT INTO table1 (col2, col3)
VALUES ('datB2', 'datC2');
But it adds new rows, like below:但它添加了新行,如下所示:
col1 col2 col3
datA1 datB1 datC1
datA2
NULL datB2 datC2
What I really need is to fill the row starting with value "datA2" at the column "col1" with new values and get the table like below:我真正需要的是用新值填充列“col1”中以值“datA2”开头的行,并得到如下表:
col1 col2 col3
datA1 datB1 datC1
datA2 datB2 datC2
The table has 3 columns and each column responses for a particular type of values (say: name, colour, size).该表有 3 列,每列响应特定类型的值(例如:名称、颜色、大小)。 What I need is just a possibility to add new values at any time in a particular column and have them without Null and new rows if it has a free cell before.
我需要的只是一种在特定列中随时添加新值的可能性,并且如果之前有空闲单元格,则它们没有 Null 和新行。
UPDATE table1
SET col2 = dataB2, col3 = dataC2
WHERE col1 = dataA2;
This may serve your purpose.这可能有助于您的目的。
You will have to use the UPDATE
statement if you want to add data to an existing row.如果要将数据添加到现有行,则必须使用
UPDATE
语句。 Like this for example:像这样例如:
UPDATE table1 SET
col2 = 'data5'
col3 = 'data6'
FROM table1
WHERE col1 = 'data4'
Also it does look like the root of your problem is poor database design, but this query is just to show you how to add data to an existing row.此外,看起来您的问题的根源确实是糟糕的数据库设计,但此查询只是为了向您展示如何将数据添加到现有行。
I found the solution (a chain of logical operations):我找到了解决方案(一系列逻辑运算):
1) CHECK
if there is a cell (in the target column) with values either ""
or NULL
. 1)
CHECK
是否有一个单元格(在目标列中)的值为""
或NULL
。
2) IF
it has one of those then rewrite the FIRST one keeping the values of the other cells in this row at their places (assumably we use UPDATE
))) ). 2)
IF
它有一个,然后重写第一个,将这一行中其他单元格的值保留在它们的位置(假设我们使用UPDATE
))) )。
3) ELSE
just add a new row with all NULL
s in the other cell in the row. 3)
ELSE
只需在该行的另一个单元格中添加一个新行,其中所有NULL
s。
If we want to add a few values into various columns simultaneously we may prepare our queries for all of those and then execute them simultaneously (sorry for tautology).如果我们想同时将一些值添加到不同的列中,我们可以为所有这些准备我们的查询,然后同时执行它们(对不起,重言式)。
If we need to add a few values into the same column within one query we can prepare it, using loops (repeating paragraphs 1 and 2 (or, optionally, 3).如果我们需要在一个查询中将几个值添加到同一列中,我们可以使用循环(重复第 1 段和第 2 段(或可选地,第 3 段))来准备它。
Given a table structure, with two data rows:给定一个表结构,有两个数据行:
key value
--------------------
team accounts
manager jeff
Each time you want to change a value, you need to check whether it is already there (for update), or not (for insert).每次要更改值时,都需要检查它是否已经存在(用于更新),或不存在(用于插入)。 So, to change the value of the
manager
property:因此,要更改
manager
属性的值:
if exists(select * from keyValues where key = 'manager')
update keyValues set value = 'mike' where key = 'manager'
else
insert into keyValues ('manager', 'mike')
Suppose you have the table假设你有一张桌子
CLIENT_MASTER CLIENT_MASTER
ClientNo Name客户名称
C00001 Ivan C00001 伊万
C00002 Himanshu C00002 喜满树
Now you add a new column City现在您添加一个新列 City
ALTER table CLIENT_MASTER
ADD( City varchar(10));
Now if you want to add values in already existing rows you can use UPDATE command.现在,如果您想在现有行中添加值,您可以使用 UPDATE 命令。
For example例如
UPDATE CLIENT_MASTER
SET City='Delhi'
WHERE ClientNo='C00001';
The updated table is更新后的表是
ClientNo Name City客户无名城市
C00001 Ivan Delhi C00001 伊万德里
C00002 Himanshu NULL C00002 喜满树 NULL
Use this:用这个:
INSERT INTO table1 (col2, col3)
VALUES ('datB2', 'datC2')
WHERE col1 = datA2;
Use this, if value null
then insert empty value.使用这个,如果值为
null
则插入空值。
$cal1=$cal1 ? "data5" : '';
$cal2=$cal2 ? "data6" : '';
INSERT INTO table1 (col2, col3)
VALUES ("'.$cal1.'", "'.$cal2.'");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.