Solved

complex mysql query

Posted on 2007-11-27
7
1,115 Views
Last Modified: 2012-08-13
I am trying to come up with a solution for my query below. heres my problem. the query below is on two tables. the second table "orders_products" can have multiple entries tied to the first table "orders". What I want to do is have a result that counts the sum of the entries in "orders_products" based on an extra id and returns those counts per row. For example.

orders contain the order_id and purchase date, and orders_products contain the products that were purchased with columns like order_id and product_id. I am trying to create a query that will report all products purchased within a timeframe and if any of the "product_id" match, count and combine those products into results per row. So my end result should look something like

row1 = sum of product_id1 - product_name1
row2 = sum of product_id2 - product name2.

heres what I have so far which basically returns every order within the date range.


$query = "SELECT o.*, op.*
FROM `orders` o, `orders_products op
WHERE o.orders_id = op.orders_id
AND date_purchased >= '2007-11-06'
AND date_purchased <= '2007-11-07'";
                                                                              
$data = mysql_query($query);
$rows = 0;
 while ($record = mysql_fetch_array($data) ) {
  $rows++;
  $records[] = $record;
}
0
Comment
Question by:redcable
  • 3
  • 2
7 Comments
 
LVL 11

Expert Comment

by:elfe69
ID: 20358475
You should perform a GROUP BY:

SELECT o.orders_id, o.date_purchased, SUM(op.orders_products_id) AS products_count
FROM `orders` o, `orders_products op
WHERE o.orders_id = op.orders_id
AND date_purchased >= '2007-11-06'
AND date_purchased <= '2007-11-07
GROUP BY o.orders_id, o.date_purchased

You can see the GROUP BY reference here: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
0
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20361633

$query = "

SELECT op.product_id, count(product_id) as CountSold, p.product_name

FROM `orders` o, `orders_products` op, products p

WHERE o.orders_id = op.orders_id

AND p.product_id = op.product_id

AND date_purchased >= '2007-11-06'

AND date_purchased <= '2007-11-07'

group by op.product_id";

Open in new window

0
 

Author Comment

by:redcable
ID: 20361810
end up with this, i think the above would work as well so accepting.

SELECT products_id, count(products_id)
FROM orders_products
JOIN orders on (orders_products.orders_id = orders.orders_id)
WHERE orders.date_purchased between '2007-11-01' and '2007-11-07'
GROUP BY orders_products.products_id
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 25

Expert Comment

by:imitchie
ID: 20362552
the above DOESN't work.

SUM(op.orders_products_id) AS products_count

gives you ID+ID+ID+ID instead of counting them.. fffff
0
 

Author Comment

by:redcable
ID: 20362648
oops imitchie i meant to accept yours, anyway t change or to late?
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20363134
gracias
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Database Design Concept 3 52
mySql Syntax 7 44
MySQL Select latest stock count records 11 57
Multilanguage Database Design in MySQL 5 73
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now