Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

complex mysql query

Posted on 2007-11-27
7
Medium Priority
?
1,121 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

610 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