Solved

complex mysql query

Posted on 2007-11-27
7
1,116 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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 …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

11 Experts available now in Live!

Get 1:1 Help Now