我一直在寻找一种方法将这个查询/ php转换为INSERT INTO SELECT,但我找不到一种方法来为查询的concat部分添加值,任何建议
$query = tep_db_query("SELECT DISTINCT products_attributes.products_id AS products_id, GROUP_CONCAT(DISTINCT 'filter_size_', products_attributes.options_id, '_', filter_options.filter_options_id) AS size FROM products_attributes INNER JOIN filter_options_to_values ON products_attributes.options_id = filter_options_to_values.options_id AND products_attributes.options_values_id = filter_options_to_values.values_id INNER JOIN filter_options ON filter_options_to_values.filter_options_id = filter_options.filter_options_id GROUP BY products_attributes.products_id;");
tep_db_query("TRUNCATE table " . $tableName);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$array = explode(',', $row['size']);
$count = count($array);
$string = '';
foreach ($array as $a) {
$string .= '1,';
}
$string = rtrim($string, ',');
tep_db_query("INSERT INTO " . $tableName . " (products_id, " . $row['size'] . ") VALUES (" . $row['products_id'] . ", " . $string . ")");
}
这是我正在尝试插入但问题是dynimic cols大小:
SELECT DISTINCT
products_attributes.products_id,
filter_options_to_values.filter_options_id,
GROUP_CONCAT(DISTINCT 'filter_size_', products_attributes.options_id, '_', filter_options.filter_options_id) AS size,
GROUP_CONCAT(DISTINCT '1') AS vals
FROM
products_attributes
INNER JOIN filter_options_to_values ON products_attributes.options_id = filter_options_to_values.options_id AND products_attributes.options_values_id = filter_options_to_values.values_id
INNER JOIN filter_options ON filter_options_to_values.filter_options_id = filter_options.filter_options_id
WHERE products_attributes.options_quantity > 0
GROUP BY products_attributes.products_id
这是架构:
CREATE TABLE
products_temp (
products_id int(11) NOT NULL default '0',
final_price decimal(15,4) default NULL,
special_price decimal(15,4) default NULL,
manufacturers_id int(11) default NULL,
efv4 int(11) default NULL,
efv5 int(11) default NULL,
efv10 int(11) default NULL,
products_date_added datetime default NULL,
filter_size_2_1 int(11) default NULL,
filter_size_2_2 int(11) default NULL,
filter_size_2_3 int(11) default NULL,
filter_size_2_4 int(11) default NULL,
filter_size_2_5 int(11) default NULL,
filter_size_2_6 int(11) default NULL,
filter_size_2_7 { {1}} filter_size_2_8 int(11) default NULL,
filter_size_2_9 int(11) default NULL,
filter_size_2_10 int(11) default NULL,
filter_size_2_11 int(11) default NULL,
filter_size_2_12 int(11) default NULL,
filter_size_2_57 int(11) default NULL,
filter_size_2_58 int(11) default NULL,
filter_size_2_59 {{1 }} filter_size_2_60 int(11) default NULL,
filter_size_2_62 int(11) default NULL,
filter_size_2_63 int(11) default NULL,
filter_size_2_293 int(11) default NULL,
filter_size_2_294 int(11) default NULL,
filter_size_2_295 int(11) default NULL,
filter_size_2_296 int(11) default NULL,
filter_size_9_20 int(11) default NULL,
filter_size_9_21 int(11) default NULL,
filter_size_9_24 int(11) default NULL,
filter_size_9_25 int(11) default NULL,
filter_size_9_26 int(11) default NULL,
filter_size_9_27 int(11) default NULL,
filter_size_9_28 int(11) default NULL,
filter_size_9_29 int(11) default NULL,
filter_size_9_30 int(11) default NULL,
filter_size_9_31 { {1}} filter_size_9_32 int(11) default NULL,
filter_size_9_33 int(11) default NULL,
filter_size_9_34 int(11) default NULL,
filter_size_9_35 int(11) default NULL,
filter_size_9_36 int(11) default NULL,
filter_size_9_37 int(11) default NULL,
filter_size_9_38 int(11) default NULL,
filter_size_9_39 {{1 }} filter_size_9_40 int(11) default NULL,
filter_size_9_41 int(11) default NULL,
filter_size_9_42 int(11) default NULL,
filter_size_9_43 int(11) default NULL,
filter_size_9_44 int(11) default NULL,
filter_size_9_45 int(11) default NULL,
filter_size_9_46 int(11) default NULL,
filter_size_9_47 int(11) default NULL,
filter_size_9_48 {{ 1}} filter_size_9_49 int(11) default NULL,
filter_size_9_50 int(11) default NULL,
filter_size_9_51 int(11) default NULL,
filter_size_9_52 int(11) default NULL,
filter_size_9_53 int(11) default NULL,
filter_size_9_54 int(11) default NULL,
filter_size_9_55 int(11) default NULL,
filter_size_9_56 {{1} } {products_id {1}}
答案 0 :(得分:0)
您的SELECT
语句有4个字段。您尝试插入的表格类似于50.您的SELECT
语句需要有50个字段,或者您需要将临时表更改为4个字段以匹配SELECT的结果。我的赌注是你的表格架构不是那么好,也许SELECT的结果就是你真正想要的表格。
您在SELECT中包含filter_options_id
,因此也应该聚合,或者它应该在GROUP BY中。否则将意味着如果每个filter_options_id
有多个products_id
,您的数据库将只抓取随机filter_options_id
以包含在记录中。如果每个filter_options_id
只有一个不同的products_id
,那么只需将其添加到GROUP BY即可。如果您曾升级到MySQL 5.7,则此查询将会出错。
无需在同一查询中使用DISTINCT
和GROUP BY
。您使用GROUP_CONCAT
汇总了其中一列,因此GROUP BY
是正确的选择。在DISTINCT
之后删除SELECT
。
GROUP_CONCAT(DISTINCT 1) as vals
与1 as vals
我唯一的另一个想法是,也许你的目标是用PIVOT查询来填充你的表,这也许就是为什么你要追求group_concat。 Check out this question for information on how to pull that off.