create table customers
(
custID int(11) primary key,
customerName varchar(50),
phone varchar(50),
address varchar(50),
state varchar(50),
postalCode varchar(15),
country varchar(50),
creditLimit Decimal(10,2)
);
create table product
(
productID varchar(15) primary key,
productName varchar(70),
productVendor varchar(50),
productDescription text,
qtyInStock smallint(6),
price Decimal(10,2)
);
create table orders
(
orderNumber int(11) primary key,
orderDate date,
requiredDate date,
shippedDate date,
status varchar(15),
comments text,
custID int(11),
salesID int(11),
foreign key(custID) references customers(custID),
foreign key(salesID) references salespersonnel(employeeID)
);
here are three tables, i want to fetch all of the orders of one particular customer and their total money spent.
I have two entries in order table and payment table
I am executing this query
select count(p.orderNumber) as 'Total Orders', sum(amount) as 'Amount Spent'
from orders p,
payments q
where p.custid = q.custid
and q.custid = 1;
This query is generating double result that means total of 2200 must be returned as the total amount spent, but i am getting 4400 and total 2 orders are placed by customer, but i am getting 4. Please help


GROUP BYshould help youJOINsyntax. Easier to write (without errors), easier to read (and maintain), and easier to convert to outer join if needed.