Like运算符中的变量或内部选择?

时间:2014-05-24 10:08:24

标签: mysql sql

我需要在表中进行一些更新,并且我想基于类似的语句更新一些值。例如'%0010030 a.jpg' 0010030是我数据库中产品代码的一个示例,它到目前为止已达到特定数量。

我想做一个for循环,将i = 0010030的变化改为我需要的数字,i ++。 我不确定它是否有效,但我试图像这样设置它'%Variable_Name a.jpg' 为了自动完成。

我实际上有一个我需要检查的sku / id列表 任何帮助或想法?

我有什么:

Insert  Ignore Into prefix_virtuemart_product_medias(virtuemart_product_id,virtuemart_media_id,ordering) VALUES (
(Select nprefix_virtuemart_products.virtuemart_product_id from prefix_virtuemart_products where prefix_virtuemart_products.product_sku LIKE '0010030'),
(Select prefix_virtuemart_medias.virtuemart_media_id from prefix_virtuemart_medias 
where  prefix_virtuemart_medias.file_url LIKE '%0010030 a.jpg' or prefix_virtuemart_medias.file_url LIKE '%0010030 A.jpg'),5);

但是我需要从包含所有这些产品代码的列中自动更改LIKE参数(它是product_sku字段)

包含样本数据的表转储:

CREATE TABLE IF NOT EXISTS `prefix_virtuemart_product_medias` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `virtuemart_product_id` int(1) unsigned NOT NULL DEFAULT '0',
  `virtuemart_media_id` int(1) unsigned NOT NULL DEFAULT '0',
  `ordering` int(2) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `i_virtuemart_product_id` (`virtuemart_product_id`,`virtuemart_media_id`),
  KEY `i_ordering` (`ordering`)
) ENGINE=MyISAM AUTO_INCREMENT=3443 DEFAULT CHARSET=utf8;

-- Dumping data for table virtuemart_product_medias: 1.496 rows
/*!40000 ALTER TABLE `prefix_virtuemart_product_medias` DISABLE KEYS */;
INSERT INTO `prefix_virtuemart_product_medias` (`id`, `virtuemart_product_id`, `virtuemart_media_id`, `ordering`) VALUES
    (1909, 2849, 12595, 1),
    (1910, 2849, 12596, 1),
    (1911, 2849, 12597, 1),


-- Dumping structure for table virtuemart_medias
CREATE TABLE IF NOT EXISTS `prefix_virtuemart_medias` (
  `virtuemart_media_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `virtuemart_vendor_id` smallint(1) NOT NULL DEFAULT '1',
  `file_title` char(126) NOT NULL DEFAULT '',
  `file_description` char(254) NOT NULL DEFAULT '',
  `file_meta` char(254) NOT NULL DEFAULT '',
  `file_mimetype` char(64) NOT NULL DEFAULT '',
  `file_type` char(32) NOT NULL DEFAULT '',
  `file_url` varchar(900) NOT NULL DEFAULT '',
  `file_url_thumb` varchar(900) NOT NULL DEFAULT '',
  `file_is_product_image` tinyint(1) NOT NULL DEFAULT '0',
  `file_is_downloadable` tinyint(1) NOT NULL DEFAULT '0',
  `file_is_forSale` tinyint(1) NOT NULL DEFAULT '0',
  `file_params` varchar(17500) DEFAULT NULL,
  `file_lang` varchar(500) NOT NULL,
  `shared` tinyint(1) NOT NULL DEFAULT '0',
  `published` tinyint(1) NOT NULL DEFAULT '1',
  `created_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `created_by` int(11) NOT NULL DEFAULT '0',
  `modified_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `modified_by` int(11) NOT NULL DEFAULT '0',
  `locked_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `locked_by` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`virtuemart_media_id`),
  KEY `i_virtuemart_vendor_id` (`virtuemart_vendor_id`),
  KEY `i_published` (`published`),
  KEY `i_shared` (`shared`)
) ENGINE=MyISAM AUTO_INCREMENT=16811 DEFAULT CHARSET=utf8 COMMENT='Additional Images and Files which are assigned to products';

-- Dumping data for table virtuemart_medias: 4.216 rows
/*!40000 ALTER TABLE `prefix_virtuemart_medias` DISABLE KEYS */;
INSERT INTO `prefix_virtuemart_medias` (`virtuemart_media_id`, `virtuemart_vendor_id`, `file_title`, `file_description`, `file_meta`, `file_mimetype`, `file_type`, `file_url`, `file_url_thumb`, `file_is_product_image`, `file_is_downloadable`, `file_is_forSale`, `file_params`, `file_lang`, `shared`, `published`, `created_on`, `created_by`, `modified_on`, `modified_by`, `locked_on`, `locked_by`) VALUES
    (12595, 1, '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', 'image/jpeg', 'product', 'images/stories/virtuemart/product/0010030 a.jpg', '', 0, 0, 0, '', '', 0, 1, '2014-05-10 06:06:44', 466, '2014-05-23 14:09:03', 466, '0000-00-00 00:00:00', 0),
    (12596, 1, '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', 'image/jpeg', 'product', 'images/stories/virtuemart/product/0010030 b.jpg', '', 0, 0, 0, '', '', 0, 1, '2014-05-10 06:06:44', 466, '2014-05-23 14:09:03', 466, '0000-00-00 00:00:00', 0),
    (12597, 1, '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', 'image/jpeg', 'product', 'images/stories/virtuemart/product/0010030 c.jpg', '', 0, 0, 0, '', '', 0, 1, '2014-05-10 06:06:44', 466, '2014-05-23 14:09:03', 466, '0000-00-00 00:00:00', 0),
    (12598, 1, '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', '0010030 MIZA DAIHATSU', 'image/jpeg', 'product', 'images/stories/virtuemart/product/0010030 d.jpg', '', 0, 0, 0, '', '', 0, 1, '2014-05-10 06:06:44', 466, '2014-05-23 14:09:03', 466, '0000-00-00 00:00:00', 0);

-- Dumping structure for table prefix_virtuemart_products
CREATE TABLE IF NOT EXISTS `prefix_virtuemart_products` (
  `virtuemart_product_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `virtuemart_vendor_id` smallint(1) unsigned NOT NULL DEFAULT '1',
  `product_parent_id` int(1) unsigned NOT NULL DEFAULT '0',
  `product_sku` char(64) DEFAULT NULL,
  `product_gtin` char(64) DEFAULT NULL,
  `product_mpn` char(64) DEFAULT NULL,
  `product_weight` decimal(10,4) DEFAULT NULL,
  `product_weight_uom` char(7) DEFAULT NULL,
  `product_length` decimal(10,4) DEFAULT NULL,
  `product_width` decimal(10,4) DEFAULT NULL,
  `product_height` decimal(10,4) DEFAULT NULL,
  `product_lwh_uom` char(7) DEFAULT NULL,
  `product_url` char(255) DEFAULT NULL,
  `product_in_stock` int(1) NOT NULL DEFAULT '0',
  `product_ordered` int(1) NOT NULL DEFAULT '0',
  `low_stock_notification` int(1) unsigned NOT NULL DEFAULT '0',
  `product_available_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `product_availability` char(32) DEFAULT NULL,
  `product_special` tinyint(1) DEFAULT NULL,
  `product_sales` int(1) unsigned NOT NULL DEFAULT '0',
  `product_unit` varchar(8) DEFAULT NULL,
  `product_packaging` decimal(8,4) unsigned DEFAULT NULL,
  `product_params` varchar(2000) DEFAULT NULL,
  `hits` int(11) unsigned DEFAULT NULL,
  `intnotes` varchar(18000) DEFAULT NULL,
  `metarobot` varchar(400) DEFAULT NULL,
  `metaauthor` varchar(400) DEFAULT NULL,
  `layout` char(16) DEFAULT NULL,
  `published` tinyint(1) DEFAULT NULL,
  `pordering` mediumint(2) unsigned NOT NULL DEFAULT '0',
  `created_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `created_by` int(11) NOT NULL DEFAULT '0',
  `modified_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `modified_by` int(11) NOT NULL DEFAULT '0',
  `locked_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `locked_by` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`virtuemart_product_id`),
  KEY `idx_product_virtuemart_vendor_id` (`virtuemart_vendor_id`),
  KEY `idx_product_product_parent_id` (`product_parent_id`),
  KEY `i_product_special` (`product_special`),
  KEY `i_published` (`published`),
  KEY `i_pordering` (`pordering`)
) ENGINE=MyISAM AUTO_INCREMENT=3809 DEFAULT CHARSET=utf8 COMMENT='All products are stored here.';

-- Dumping data for table kontosdb.prefix_virtuemart_products: 960 rows
/*!40000 ALTER TABLE `prefix_virtuemart_products` DISABLE KEYS */;
INSERT INTO `prefix_virtuemart_products` (`virtuemart_product_id`, `virtuemart_vendor_id`, `product_parent_id`, `product_sku`, `product_gtin`, `product_mpn`, `product_weight`, `product_weight_uom`, `product_length`, `product_width`, `product_height`, `product_lwh_uom`, `product_url`, `product_in_stock`, `product_ordered`, `low_stock_notification`, `product_available_date`, `product_availability`, `product_special`, `product_sales`, `product_unit`, `product_packaging`, `product_params`, `hits`, `intnotes`, `metarobot`, `metaauthor`, `layout`, `published`, `pordering`, `created_on`, `created_by`, `modified_on`, `modified_by`, `locked_on`, `locked_by`) VALUES
    (2849, 1, 0, '0100300', '', '', 0.0000, 'KG', 0.0000, 0.0000, 0.0000, 'CM', '', 0, 0, 0, '2010-05-17 00:00:00', '', 0, 0, 'KG', NULL, 'min_order_level=""|max_order_level=""|step_order_level=""|product_box=""|', NULL, '', '', '', '0', 1, 0, '2010-05-17 12:46:52', 466, '2014-05-16 06:35:16', 466, '0000-00-00 00:00:00', 0),
    (2850, 1, 0, '0100623', NULL, NULL, 0.0000, 'KG', 0.0000, 0.0000, 0.0000, 'CM', '', 0, 0, 0, '2010-05-28 00:00:00', '', 0, 0, 'Τεμάχιο', NULL, 'min_order_level=null|max_order_level=null|step_order_level=null|product_box=null|', NULL, '', '', '', '', 1, 0, '2010-05-28 12:40:19', 466, '2014-05-10 06:06:44', 466, '0000-00-00 00:00:00', 0),
    (2851, 1, 0, '0100630', NULL, NULL, 0.0000, 'KG', 0.0000, 0.0000, 0.0000, 'CM', '', 0, 0, 0, '2010-05-28 00:00:00', '', 0, 0, 'Τεμάχιο', NULL, 'min_order_level=null|max_order_level=null|step_order_level=null|product_box=null|', NULL, '', '', '', '', 1, 0, '2010-05-28 12:47:16', 466, '2014-05-10 06:06:44', 466, '0000-00-00 00:00:00', 0);

1 个答案:

答案 0 :(得分:0)

我不确定我明白你遇到了什么问题。通过以下内容为您提供您期望的结果?

SELECT
    nprefix_virtuemart_products.virtuemart_product_id,
    pvm.virtuemart_media_id,
    5
FROM
    prefix_virtuemart_products pvp
    INNER JOIN prefix_virtuemart_medias pvm ON pvm.file_url LIKE CONCAT('%', pvp.product_sku, ' a.jpg') 
                                            OR pvm.file_url LIKE CONCAT('%', pvp.product_sku, ' A.jpg');

要使这个工作,子查询只能返回一行,我不知道你的数据是什么样的,所以可能不是这样。