MYSQL子选择不起作用

时间:2011-12-31 14:10:53

标签: mysql

我试图在一个中运行两个查询。当我运行两个查询seperatley时,他们工作并向我提供我想要的相关信息。当我尝试将它们组合起来时,我似乎在某处出错了。有什么明显的事情表明我做错了吗?

 SELECT 
   pd.product_id, b.product_id, basket_qty, 
   product_name, product_price, product_image, 
   pd.category_id, basket_id
 (SELECT 
   pd.product_id, b.product_id, 
   basket_session_id, 
   SUM(product_price) AS subtotal 
  FROM 
   basket b, product pd 
  WHERE 
   basket_Session_id = '9htt961lpa1kqieogd5ig5ff93' AND 
   b.product_id = pd.product_id)
 FROM 
   basket b, product pd, department dep
 WHERE 
   basket_session_id = '9htt961lpa1kqieogd5ig5ff93' 
   AND b.product_id = pd.product_id 
   AND dep.department_id = pd.category_id

表格结构 -

CREATE TABLE IF NOT EXISTS `basket` (
  `basket_id` int(10) unsigned NOT NULL auto_increment,
  `product_id` int(10) unsigned NOT NULL,
  `basket_qty` int(10) unsigned NOT NULL default '1',
  `basket_session_id` char(32) NOT NULL default '',
  `basket_date` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`basket_id`),
  KEY `product_id` (`product_id`),
  KEY `basket_session_id` (`basket_session_id`)
)

CREATE TABLE IF NOT EXISTS `product` (
  `product_id` int(10) unsigned NOT NULL auto_increment,
  `category_id` int(10) unsigned NOT NULL,
  `department_name` varchar(100) NOT NULL,
  `product_name` varchar(100) NOT NULL default '',
  `product_description` text NOT NULL,
  `product_price` decimal(7,2) NOT NULL default '0.00',
  `product_qty` smallint(5) unsigned NOT NULL default '0',
  `product_size` text NOT NULL,
  `product_image` varchar(200) default NULL,
  `product_date` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`product_id`),
  UNIQUE KEY `product_name` (`product_name`),
  KEY `category_id` (`category_id`)
)


CREATE TABLE IF NOT EXISTS `department` (
  `department_id` int(10) unsigned NOT NULL auto_increment,
  `department_parent_id` int(11) NOT NULL default '0',
  `name` varchar(50) NOT NULL default '',
  `description` varchar(200) NOT NULL default '',
  `image` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`department_id`),
  UNIQUE KEY `name` (`name`),
  KEY `department_parent_id` (`department_parent_id`)
)

我正在尝试根据电子商务应用程序的“篮子”会话提取产品信息。由于篮子可以包含多个产品,我想从所有产品中返回总价值的总和。

2 个答案:

答案 0 :(得分:3)

确保在子选择之前添加逗号..

SELECT 
   pd.product_id, b.product_id, basket_qty, 
   product_name, product_price, product_image, 
   pd.category_id, basket_id,  
 (SELECT ...

除此之外,您的查询对我来说看起来不合适。你到底想要达到什么目的?我确信有比你刚写的更好的查询。

所以我不确定你想要实现什么,我不知道你的数据库是怎样的,但是修改这个查询以满足你的需求。这应该是一个很好的开始:

SELECT pd.product_id, b.product_id, b.basket_qty,
       pd.product_name, pd.product_price, pd.product_image,
       pd.category_id, b.basket_id, b.basket_session_id, 
       SUM (pd.product_price) AS subtotal
FROM product pd
JOIN basket b ON b.product_id = pd.product_id
JOIN department dep ON dep.department_id = pd.category_id
WHERE b.basket_session_id = '9htt961lpa1kqieogd5ig5ff93'
GROUP BY product_id

另请注意,您使用的是department表,但从不从中选择任何字段。所以这里似乎毫无用处。我刚刚在我的查询中添加了它,以防您以后想要改进它。

答案 1 :(得分:0)

这不起作用,因为子查询实际上是整个查询中返回的列,但是,子查询返回多个列。您可以删除额外的列并执行此操作,这应该可以。

 SELECT pd.product_id, b.product_id, basket_qty
      , product_name, product_price, product_image
      , pd.category_id, basket_id
        -- moved from sub-query
      , basket_session_id 
      , ( SELECT SUM(product_price) 
            FROM basket b, product pd 
           WHERE basket_Session_id = '9htt961lpa1kqieogd5ig5ff93' 
             AND b.product_id = pd.product_id ) AS subtotal
  FROM basket b, product pd, department dep
 WHERE basket_session_id = '9htt961lpa1kqieogd5ig5ff93' 
   AND b.product_id = pd.product_id 
   AND dep.department_id = pd.category_id