CanMan2004 Posted December 28, 2006 Share Posted December 28, 2006 Hi allNot sure if anyone can help me, but I have quite a complex query to write and need some help.Basically I have a table of products called "PRODS", an example table looks likeID AREA TYPE TITLE PRICE1 3 1 IPOD NANO 6y865g2 5 2 DELL LAPTOP c4tg853 3 1 Carry Case 7uhb54I then have a table of prices called "PROD_PRICES", an example looks likeID CODE PRICE1 6y865g 114.762 3gg754 23.003 c4tg85 566.774 7uhb54 234.00As you will notice, the field "CODE" in the above table ("PROD_PRICES") contains a code which relates to the codes held in the field "CODE" in the "PRODS" table.What I want to do is to perform a query, which would take all products from the "PRODS" table which has a value of "3" under the field "AREA" and a value of "1" under the field "TYPE", for that query, I would do[code]SELECT * FROM `prods` WHERE `area`='3' AND `type`='1'[/code]With the table "PRODS", it would return rows with ID numbers "1" and "3".What I want to do with the data now, is to take the code held in the "PRICE" field for each row returned and then lookup that code in the table "PROD_PRICES" and return the value "PRICE", but then add them up, so that it gives me a total value.If I returned rows "1" and "3" from the "PRODS" table, then I would getID AREA TYPE TITLE PRICE1 3 1 IPOD NANO 6y865g3 3 1 Carry Case 7uhb54I would then take the "PRICE" code for each row returned, so row 1 code is "6y865g", so it would look that up in the table "PROD_PRICES" and returnID CODE PRICE1 6y865g 114.76and then it would take the code from row 2 "7uhb54" and returnID CODE PRICE4 7uhb54 234.00It would then take the 2 values in the "PRICES" field114.76and234.00and add them up, to make the value348.76Does that make sense?Can anyone help?Thanks in advance everyoneDave Link to comment https://forums.phpfreaks.com/topic/32005-complex-total-count/ Share on other sites More sharing options...
utexas_pjm Posted December 28, 2006 Share Posted December 28, 2006 If I'm not oversimplifying your problem this should work:[code]SELECT SUM(pp.price) as sum FROM `prod_prices` as pp LEFT JOIN `prods` as p ON (pp.code = p.price) WHERE p.area='3' AND p.type='1'[/code]Best, Patrick Link to comment https://forums.phpfreaks.com/topic/32005-complex-total-count/#findComment-148561 Share on other sites More sharing options...
ted_chou12 Posted December 28, 2006 Share Posted December 28, 2006 here... i dont know if it will work or not, but it starts you off<?php//get the info from the first table && specify your standards$result = mysql_query("SELECT * FROM 'prods' WHERE 'area'='3' AND 'type'='1'") or die(mysql_error()); //using the while function, putting subs into arrayswhile($row = mysql_fetch_array($result)){$result = mysql_query("SELECT * FROM 'prod_prices' WHERE 'code'='$row['price']'") or die(mysql_error()); $row = mysql_fetch_array($result);$subtotals[] = $row['price'];}//add them up to get the total...$total = 0;foreach ($subtotals as $st){$total = $total + $st;}echo "$total\n";?> Link to comment https://forums.phpfreaks.com/topic/32005-complex-total-count/#findComment-148562 Share on other sites More sharing options...
CanMan2004 Posted December 28, 2006 Author Share Posted December 28, 2006 thanks, your help has been great Link to comment https://forums.phpfreaks.com/topic/32005-complex-total-count/#findComment-148563 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.