使用来自另一列的符合特定条件的值更新一列
Update a column using values from another column that match a specific criteria
我想删除字段 customers_fax
中的所有条目,然后将 07
开头的所有数字从 customers_telephone
字段移动到 customers_fax
字段。
table结构如下
CREATE TABLE IF NOT EXISTS `zen_customers` (
`customers_id` int(11) NOT NULL,
`customers_gender` char(1) NOT NULL DEFAULT '',
`customers_firstname` varchar(32) NOT NULL DEFAULT '',
`customers_lastname` varchar(32) NOT NULL DEFAULT '',
`customers_dob` datetime NOT NULL DEFAULT '0001-01-01 00:00:00',
`customers_email_address` varchar(96) NOT NULL DEFAULT '',
`customers_nick` varchar(96) NOT NULL DEFAULT '',
`customers_default_address_id` int(11) NOT NULL DEFAULT '0',
`customers_telephone` varchar(32) NOT NULL DEFAULT '',
`customers_fax` varchar(32) DEFAULT NULL,
`customers_password` varchar(40) NOT NULL DEFAULT '',
`customers_newsletter` char(1) DEFAULT NULL,
`customers_group_pricing` int(11) NOT NULL DEFAULT '0',
`customers_email_format` varchar(4) NOT NULL DEFAULT 'TEXT',
`customers_authorization` int(1) NOT NULL DEFAULT '0',
`customers_referral` varchar(32) NOT NULL DEFAULT '',
`customers_paypal_payerid` varchar(20) NOT NULL DEFAULT '',
`customers_paypal_ec` tinyint(1) unsigned NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=18346 ;
删除任何现有数据非常简单,我会这样做
UPDATE zen_customers SET customers_fax = ''
我不知道如何仅将以 07
开头的数字移动到 customers_fax
字段以确保它们与相关的 customers_id
.
保持一致
是否有一种简单的方法可以仅作为 SQL 查询来执行此操作?
尝试这样的事情:
UPDATE zen_customers
SET customers_fax = customers_telephone
WHERE customers_telephone like '07%'
这应该有效:
UPDATE zen_customers
SET customers_fax = customers_telephone
WHERE customers_telephone LIKE '07%';
但是如果您想对一位客户进行测试以确保它符合您的预期,您可以随时运行对电话号码为“07”的特定用户进行此更新查询:
UPDATE zen_customers
SET customers_fax = customers_telephone
WHERE customers_id = N;
其中 N 是一个整数。
UPDATE zen_customers a, zen_customers b
SET a.customers_fax=b.customers_telephone
WHERE
a.customers_id=b.customers_id
AND b.customers_telephone LIKE '07%'
如果 customers_telephone
以 07
开头,您可以使用 case
表达式有条件地将 customers_fax
设置为 customers_telephone
,如果 ''
如果它没有:
UPDATE zen_customers
SET customers_fax = CASE
WHEN customers_telephone LIKE '07%' THEN customers_telephone
ELSE ''
END;
这将使您不必 运行 两次不同的更新。
我想删除字段 customers_fax
中的所有条目,然后将 07
开头的所有数字从 customers_telephone
字段移动到 customers_fax
字段。
table结构如下
CREATE TABLE IF NOT EXISTS `zen_customers` (
`customers_id` int(11) NOT NULL,
`customers_gender` char(1) NOT NULL DEFAULT '',
`customers_firstname` varchar(32) NOT NULL DEFAULT '',
`customers_lastname` varchar(32) NOT NULL DEFAULT '',
`customers_dob` datetime NOT NULL DEFAULT '0001-01-01 00:00:00',
`customers_email_address` varchar(96) NOT NULL DEFAULT '',
`customers_nick` varchar(96) NOT NULL DEFAULT '',
`customers_default_address_id` int(11) NOT NULL DEFAULT '0',
`customers_telephone` varchar(32) NOT NULL DEFAULT '',
`customers_fax` varchar(32) DEFAULT NULL,
`customers_password` varchar(40) NOT NULL DEFAULT '',
`customers_newsletter` char(1) DEFAULT NULL,
`customers_group_pricing` int(11) NOT NULL DEFAULT '0',
`customers_email_format` varchar(4) NOT NULL DEFAULT 'TEXT',
`customers_authorization` int(1) NOT NULL DEFAULT '0',
`customers_referral` varchar(32) NOT NULL DEFAULT '',
`customers_paypal_payerid` varchar(20) NOT NULL DEFAULT '',
`customers_paypal_ec` tinyint(1) unsigned NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=18346 ;
删除任何现有数据非常简单,我会这样做
UPDATE zen_customers SET customers_fax = ''
我不知道如何仅将以 07
开头的数字移动到 customers_fax
字段以确保它们与相关的 customers_id
.
是否有一种简单的方法可以仅作为 SQL 查询来执行此操作?
尝试这样的事情:
UPDATE zen_customers
SET customers_fax = customers_telephone
WHERE customers_telephone like '07%'
这应该有效:
UPDATE zen_customers
SET customers_fax = customers_telephone
WHERE customers_telephone LIKE '07%';
但是如果您想对一位客户进行测试以确保它符合您的预期,您可以随时运行对电话号码为“07”的特定用户进行此更新查询:
UPDATE zen_customers
SET customers_fax = customers_telephone
WHERE customers_id = N;
其中 N 是一个整数。
UPDATE zen_customers a, zen_customers b
SET a.customers_fax=b.customers_telephone
WHERE
a.customers_id=b.customers_id
AND b.customers_telephone LIKE '07%'
如果 customers_telephone
以 07
开头,您可以使用 case
表达式有条件地将 customers_fax
设置为 customers_telephone
,如果 ''
如果它没有:
UPDATE zen_customers
SET customers_fax = CASE
WHEN customers_telephone LIKE '07%' THEN customers_telephone
ELSE ''
END;
这将使您不必 运行 两次不同的更新。