2013-01-07 64 views
0

我的查询是:转换一个MySQL查询的结果为一个字符串

$orderitemsattributes_query = "SELECT products_options_values 
FROM orders_products_attributes oa 
WHERE oa.orders_id 
IN (
SELECT DISTINCT o.orders_id 
FROM orders o 
WHERE o.exported =  '0' 
AND o.orders_id >  '0')"; 
$orderitemsattributes_result = mysql_query($orderitemsattributes_query); 
while($row = mysql_fetch_array($orderitemsattributes_result)) { 

echo $row['products_options_values']; 
} 

给了我下面的:

6 period day (TP6)NavyGold (j)Gold Lamé (17)LJD1 at back (free) 

我希望能够将其转换为一个字符串,用于插入数据库上的另一个字段。

我想:

$tradebox_attributes_list = implode (',', $row);` 

但我要么没有得到任何东西,或者我得到取决于存储在$ tradebox_attributes_list不完整的结果,其中我把破灭。

我需要能够沿着代码进一步读取这个变量,并在关闭之后很好地}。

表结构是:

CREATE TABLE IF NOT EXISTS `orders_products_attributes` (
    `orders_products_attributes_id` int(11) NOT NULL AUTO_INCREMENT, 
    `orders_id` int(11) NOT NULL DEFAULT '0', 
    `orders_products_id` int(11) NOT NULL DEFAULT '0', 
    `products_options` varchar(32) NOT NULL DEFAULT '', 
    `products_options_values` text NOT NULL, 
    `options_values_price` decimal(15,4) NOT NULL DEFAULT '0.0000', 
    `price_prefix` char(1) NOT NULL DEFAULT '', 
    `product_attribute_is_free` tinyint(1) NOT NULL DEFAULT '0', 
    `products_attributes_weight` float NOT NULL DEFAULT '0', 
    `products_attributes_weight_prefix` char(1) NOT NULL DEFAULT '', 
    `attributes_discounted` tinyint(1) NOT NULL DEFAULT '1', 
    `attributes_price_base_included` tinyint(1) NOT NULL DEFAULT '1', 
    `attributes_price_onetime` decimal(15,4) NOT NULL DEFAULT '0.0000', 
    `attributes_price_factor` decimal(15,4) NOT NULL DEFAULT '0.0000', 
    `attributes_price_factor_offset` decimal(15,4) NOT NULL DEFAULT '0.0000', 
    `attributes_price_factor_onetime` decimal(15,4) NOT NULL DEFAULT '0.0000', 
    `attributes_price_factor_onetime_offset` decimal(15,4) NOT NULL DEFAULT '0.0000', 
    `attributes_qty_prices` text, 
    `attributes_qty_prices_onetime` text, 
    `attributes_price_words` decimal(15,4) NOT NULL DEFAULT '0.0000', 
    `attributes_price_words_free` int(4) NOT NULL DEFAULT '0', 
    `attributes_price_letters` decimal(15,4) NOT NULL DEFAULT '0.0000', 
    `attributes_price_letters_free` int(4) NOT NULL DEFAULT '0', 
    `products_options_id` int(11) NOT NULL DEFAULT '0', 
    `products_options_values_id` int(11) NOT NULL DEFAULT '0', 
    `products_prid` tinytext NOT NULL, 
    `tradebox_attributes_list` text NOT NULL, 
    PRIMARY KEY (`orders_products_attributes_id`), 
    KEY `idx_orders_id_prod_id_zen` (`orders_id`,`orders_products_id`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=77065 ; 

Sample of one entry is: 


(76569, 22959, 34813, 'Lesson format', '6 period day (TP6)', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 5, 55, '30:499bc45895cd0baaef055a57cb36df5d', ''), 
(76570, 22959, 34813, 'Cover', 'Navy', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 1, 2, '30:499bc45895cd0baaef055a57cb36df5d', '1 at back (free)'), 
(76571, 22959, 34813, 'Wire', 'Gold (j)', 0.0000, '', 1, 0, '', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 4, 49, '30:499bc45895cd0baaef055a57cb36df5d', ''), 
(76572, 22959, 34813, 'Ribbon', 'Gold Lamé (17)', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 3, 29, '30:499bc45895cd0baaef055a57cb36df5d', ''), 
(76573, 22959, 34813, 'Initials (Max 4)', 'LJD', 3.3000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 2, 0, '30:499bc45895cd0baaef055a57cb36df5d', ''), 
(76574, 22959, 34813, 'Plastic pockets', '1 at back (free)', 0.0000, '+', 1, 0, '+', 1, 1, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, '', '', 0.0000, 0, 0.0000, 0, 6, 84, '30:499bc45895cd0baaef055a57cb36df5d', ''), 

我需要能够采取一切存储在products_options_values场的结果(注意,有六只为这个顺序),并将其作为一个逗号分隔的列表tradebox_attributes_list字段。

编辑为要求是新换的:

我对在作品中抛出一个扳手现在。 group_concat()会工作,但我刚刚被要求在单独的行上显示其中一个属性。举例来说,我们希望以下内容: 6期一天,海军,1背部(免费)NEW LINE LJD£3.30

并非如此,现在,我需要选择特定的products_options_values,然后在单独的行简单,请从options_values_price字段中选择不同的选项并关联价格。

+1

有坏设计的气味 - 为什么你会将记录加入到一个字符串中,并将它们存储在数据库的另一个地方?规范你的模式并使用'JOIN's。 – moonwave99

+0

@StevePrice是你的上述查询的输出吗? '6周日(TP6)海军黄金(j)金拉米(17)LJD1后面(免费)'不应该排成几行?然后你可以使用'group_concat'。或者你说你想用','来分割它? – bonCodigo

+0

@bonCodigo是的,需要一个由 –

回答

0

你可以用group_concat()来得到值组:order id。希望我们可以看到表虽然..

SELECT group_concat(products_options_values) 
FROM orders_products_attributes oa 
WHERE oa.orders_id 
IN (
SELECT DISTINCT o.orders_id 
FROM orders o 
WHERE o.exported = '0' 
AND o.orders_id > '0') 
group by oa.orders_id; 
相关问题