Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7016037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:43:55+00:00 2026-05-27T22:43:55+00:00

I am trying to run two queries in one. When I run both queries

  • 0

I am trying to run two queries in one. When I run both queries seperatley, they work and provide me with the relevant information I want. When I try to combine them I seem to be going wrong somewhere. Is there something blatantly obvious that I am doing wrong?

 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

Table Structure –

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`)
)

I am trying to pull product information based on a “basket” session for an ecommerce application. As a basket can contain more than one product I want to return the SUM of total value from all products.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:43:56+00:00Added an answer on May 27, 2026 at 10:43 pm

    This won’t work as the sub-query is effectively a column returned in the overall query, however, your sub-query returns more than one column. You could remove the extra columns though and do this, which should work.

     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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Situation: I'm trying run an https store (xcart) under one domain secure.example.com and I
I'm trying to run some queries to get rid of XSS in our database
I have a SQL query Question, I am trying to nest two separate queries
I'm trying to determine the relative performance of two different queries and have two
I am trying to execute my PHP code, which calls two MySQL queries via
I am trying to profile two different queries that do the same thing to
I have a database of occupation titles I'm trying to run some queries on.
I have two generic lists where I want to run a couple of Linq
I am trying to merge the result of two separate MDX queries, where the
I'm trying to run some queries to a database from a pylons (paster-based) webserver

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.