如何在mysql连接中将字段转换为字符串

时间:2016-08-29 14:20:20

标签: mysql mariadb

我尝试将一个int(13)的字段加入到varchar(50)的字段中。

如果我只使用(a.id = b.id),DESCRIBE会输入:ref。

如果我使用(a.id = CONCAT(b.id)),DESCRIBE会输入:eq_ref。 (其中b.id是整数)

使用CONCAT投放字段非常难看,因此我尝试使用CAST()CONVERT()

如果我使用(a.id = CAST(b.id AS CHAR(50))),DESCRIBE会输入:ref。

如何编写正确的强制转换/转换,这会产生一个eq_ref连接?

更新1:

DESCRIBE SELECT .. with CONCAT

+------+-------------+-----------------------+--------+-----------------------------------+----------------+---------+-------------------------------------+------+----------------------------------------+
| id   | select_type | table                 | type   | possible_keys                     | key            | key_len | ref                                 | rows | Extra                                  |
+------+-------------+-----------------------+--------+-----------------------------------+----------------+---------+-------------------------------------+------+----------------------------------------+
|    1 | SIMPLE      | ext_icecat_prodmatch  | ref    | PRIMARY,our_article_id,product_id | our_article_id | 152     | const                               | 3016 | Using index condition; Using temporary |
|    1 | SIMPLE      | ext_icecat_product    | eq_ref | PRIMARY,product_id                | PRIMARY        | 4       | ext_icecat_prodmatch.product_id     |    1 |                                        |
|    1 | SIMPLE      | ext_icecat_supplier   | eq_ref | PRIMARY                           | PRIMARY        | 4       | ext_icecat_product.supplier_id      |    1 |                                        |
|    1 | SIMPLE      | products              | eq_ref | PRIMARY                           | PRIMARY        | 152     | ext_icecat_prodmatch.our_article_id |    1 |                                        |
|    1 | SIMPLE      | partner_product_saved | eq_ref | PRIMARY                           | PRIMARY        | 155     | const,func                          |    1 | Using where                            |
|    1 | SIMPLE      | category_names        | eq_ref | PRIMARY                           | PRIMARY        | 6       | products.category_id,const          |    1 | Using where                            |
+------+-------------+-----------------------+--------+-----------------------------------+----------------+---------+-------------------------------------+------+----------------------------------------+

选择:

SELECT
    partner_product_saved.*,
    ext_icecat_product.product_id,
    CONCAT(ext_icecat_supplier.name, ' ', ext_icecat_product.name) AS export_product_name,
    ext_icecat_product.catid_match AS category_id,
    GROUP_CONCAT(ext_icecat_prodmatch.our_article_id) AS oais,
    products.file_name,
    category_names.category_path 
FROM ext_icecat_product
    LEFT JOIN ext_icecat_prodmatch USING (product_id)
    LEFT JOIN ext_icecat_supplier USING (supplier_id)
    LEFT JOIN products USING (our_article_id)
    LEFT JOIN partner_product_saved ON (partner_product_saved.partner_id = 29 AND partner_product_saved.product_id = CONCAT(ext_icecat_product.product_id))
    LEFT JOIN category_names ON (category_names.category_id = products.category_id AND category_names.language_id = 2) 
WHERE ext_icecat_prodmatch.our_article_id = '0EF03850-D25A-1174-BCDC-EC67352010A6' 
GROUP BY ext_icecat_product.product_id 
ORDER BY NULL;

显示创建表

CREATE TABLE `partner_product_saved` (
  `partner_id` mediumint(8) NOT NULL,
  `product_id` varchar(50) CHARACTER SET utf8 NOT NULL,
  `product_name` varchar(100) CHARACTER SET utf8 NOT NULL,
  `our_article_id` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
  `our_category_id` mediumint(8) DEFAULT NULL,
  `manufacture_id` mediumint(8) DEFAULT NULL,
  `manufacturer_partnr` varchar(255) COLLATE utf8_bin NOT NULL,
  `manufacturer_upc` varchar(255) COLLATE utf8_bin NOT NULL,
  `image` tinytext COLLATE utf8_bin NOT NULL,
  `image_small` tinytext COLLATE utf8_bin NOT NULL,
  `image_big` tinytext COLLATE utf8_bin NOT NULL,
  `image_200` tinytext COLLATE utf8_bin NOT NULL,
  `image_original` tinytext COLLATE utf8_bin NOT NULL,
  `image_width` int(11) DEFAULT NULL,
  `image_height` int(11) DEFAULT NULL,
  `birth` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `last_updated` timestamp NULL DEFAULT NULL,
  `saved` tinyint(3) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`partner_id`,`product_id`),
  KEY `our_article_id` (`our_article_id`),
  KEY `our_category_id` (`our_category_id`),
  KEY `manufacture_id` (`manufacture_id`,`manufacturer_partnr`),
  KEY `manufacturer_upc` (`manufacturer_upc`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE `ext_icecat_product` (
  `product_id` int(13) NOT NULL,
  `supplier_id` int(13) NOT NULL DEFAULT '0',
  `prod_id` varchar(235) COLLATE utf8_bin NOT NULL DEFAULT '',
  `prod_id_clean` varchar(255) CHARACTER SET utf8 NOT NULL,
  `catid` int(13) NOT NULL DEFAULT '0',
  `catid_match` varchar(50) CHARACTER SET utf8 NOT NULL,
  `name` varchar(255) CHARACTER SET utf8 NOT NULL,
  `name_clean` varchar(255) CHARACTER SET utf8 NOT NULL,
  `low_pic` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
  `high_pic` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
  `thumb_pic` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `family_id` int(13) NOT NULL DEFAULT '0',
  `low_pic_size` int(13) DEFAULT '0',
  `high_pic_size` int(13) DEFAULT '0',
  `thumb_pic_size` int(13) DEFAULT '0',
  `import_date` datetime NOT NULL,
  `release_date` datetime NOT NULL,
  `updated` datetime NOT NULL,
  `need_update` tinyint(1) NOT NULL DEFAULT '0',
  `deleted` tinyint(1) NOT NULL DEFAULT '0',
  `keyword` tinyint(1) NOT NULL DEFAULT '0',
  `special_match` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`product_id`),
  KEY `supplier_id` (`supplier_id`),
  KEY `catid` (`catid`),
  KEY `prod_id` (`prod_id`),
  KEY `product_id` (`product_id`,`prod_id`,`supplier_id`),
  KEY `release_Date` (`release_date`),
  KEY `prod_id_clean` (`prod_id_clean`),
  KEY `name_clean` (`name_clean`),
  KEY `need_update` (`need_update`),
  KEY `deleted` (`deleted`),
  KEY `keyword` (`keyword`),
  KEY `catid_2` (`catid`,`import_date`),
  KEY `catid_match` (`catid_match`),
  KEY `special_match` (`special_match`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

1 个答案:

答案 0 :(得分:-1)

WHERE indexed_column = any_function(any_column) - 可以使用索引。 WHERE non_indexed_column = any_function(indexed_column) - 无法使用索引。

refeq_ref之间的差异很小。我认为eq_ref是优化器决定不能有多个匹配的地方,通常是因为UNIQUE。

WHERE ext_icecat_prodmatch.our_article_id = '0EF03850-D25A-1174-BCDC-EC67352010A6' - our_article_id INDEXed?还是UNIQUE?听起来它只是一个INDEX,因此可能会出现多行。要使其eq_ref,您需要UNIQUE。但只有数据支持这样的。统计数据意味着可能有3016行与article_id。

除非您需要,否则请勿使用LEFT。请注意优化工具如何将LEFT JOIN ext_icecat_prodmatch USING (product_id)变为JOIN并决定(正确地)以ext_icecat_prodmatch开头。

回到其他讨论......

AND partner_product_saved.product_id = CONCAT(ext_icecat_product.product_id))

可以走一条路,但不能走另一条路。也就是说,它可以有效地从eip转到pps,但不是另一种方式。并且EXPLAINconst,func表示了这一点。