Counting Quantities Instead of Number of Rows - MySQL/PHP - php

I've asked a question regarding my SQL Query a while back and it worked fine until I noticed I had forgotten a very important piece to the code and now I've spent about an hour and a half trying to modify my code. The original question asked a good way to inner join two tables (orders and order_items). I then did a mysql_num_row() over the SQL Query and called it a day. I forgot that there's a cell in my order_items table named quantity. I need to integrate this into my count. I'm including my code below, and any ideas on how to easily implement this would be appreciated.
$SQL2ORDERSEARCH = "
SELECT * FROM order_items
INNER JOIN orders
ON orders.id = order_items.ord
WHERE orders.session = '$sessionID'
AND order_items.dish = '$SEARCHITEMS_OBJECT->dish'
AND order_items.size = 'full'";
$ORDERSEARCH = mysql_query($SQL2ORDERSEARCH) or die(mysql_error());
$ORDERSEARCH_NUM_ROWS = mysql_num_rows($ORDERSEARCH);
$FULLTOTAL = $FULLTOTAL + $ORDERSEARCH_NUM_ROWS;
I tried attempting a different route by doing a count. So I modified my Query as such:
$SQL2ORDERSEARCH = "
SELECT COUNT(quantity) FROM order_items
INNER JOIN orders
ON orders.id = order_items.ord
WHERE orders.session = '$sessionID'
AND order_items.dish = '$SEARCHITEMS_OBJECT->dish'
AND order_items.size = 'full'";
I don't technically need anything else since all I am trying to do is count how many dishes are involved here. I then created an if statement to figure if there was any rows coming from this query, and then calculate the quantity of them.
if($ORDERSEARCH_NUM_ROWS > 0 ) {
while($ORDERSEARCH_OBJECT = mysql_fetch_object($ORDERSEARCH)) {
$FULLQTY = COUNT($ORDERSEARCH_OBJECT->quantity);
}
}
I've gotten mixed results. Sometimes I get just straight 1's down the data table. Other times (depending on any small changes such as $FULLQTY += COUNT($ORDERSEARCH_OBJECT->quantity); and trying that) I get results where it seems almost pattern-like with the numbers increasing by 5+ and somehow adding up to near 20+ the further down the list you go.
I'm just looking for an easy way to get the count of the quantity cell in order_items, displaying them down a table, and then calculating a total. I have everything fine and dandy minus getting the count of the quantity cell in order_items. Any ideas, I'd greatly appreciate it!

Wouldn't a simple select sum(quantity) work here ?

Related

MySQL query runs out of memory

I have a system in which a store is an account and customers shop in those stores. There is a table that stores many-to-many association of customers and stores. The key attributes of that table are accountid, customerid and last_visit_date. For a set of accountids, I need to find the most recent visit of each customer. I have a query that works perfectly but seems to be inefficient because it runs out of memory for about 21000 customers.
SELECT ac.customerId FROM account_customer ac
INNER JOIN (SELECT customerId, max(last_visit_date) AS
LastVisitDate FROM account_customer
WHERE accountId in
(311,307,318,320,321,322,323,332,347,439,519,630,634,643)
GROUP BY customerId) grouped_ac
ON ac.customerId = grouped_ac.customerId
AND ac.last_visit_date = grouped_ac.LastVisitDate
AND ac.last_visit_date <= '2016-10-18'
OR ac.last_visit_date is null
When I run the above query, it gives me the correct result for a smaller dataset but for larger dataset, I get memory error. I am not even talking about a very large set - just around 20,000 + customers.
Any help would be appreciated.
Do you possibly mean
ac.customerId = grouped_ac.customerId
AND ac.last_visit_date = grouped_ac.LastVisitDate
and (ac.last_visit_date <= '2016-10-18' or ac.last_visit_date is null)
I think without the parentheses, the query may be returning all records there the last_visit_date is null.
Take a look at the answer to How exactly does using OR in a MySQL statement differ with/without parentheses?.

Dynamic logging for exports

$insert_sql = "INSERT INTO exported_leads (lead_id, partner_id) VALUES ('$id','$partner_id')
ON DUPLICATE KEY UPDATE
lead_id = VALUES(lead_id), partner_id = VALUES(partner_id), export_date = CURRENT_TIMESTAMP";
$count = $pdo->exec($insert_sql);
$count_total = $count_total + $count;
i have this code. problem is that whenever there's an existing match in the db it overwrites the partner_id with a new value. i need to keep which one exported the row when. i was thinking about adding columns to cover those, but the problem with that is i dont know how many exporters there can be. sometimes it can be 5, sometimes it can be over 20.
i currently use this query to find what i should export:
$sql = $pdo->query('SELECT p.* FROM prospects p
LEFT JOIN exported_leads e
on p.id = e.lead_id WHERE p.partner_id != '.$partner_id.' AND (e.lead_id IS NULL OR datediff(now(), e.export_date) > 90)
LIMIT '.$monthly_uttag.'');
and this also shows why i cant just paste new rows in the already exported db. because it keeps finding the first record. not the newest entry first. which screws it up.
anyhow, how would you do this and still keep a date when every partner_id exported that row?
say for example the row id=1. partner1 exported that row on 2014-01-01 and partner3 exported it on 2014-05-15. i need to keep both those dates somehow.
how would you do this? im looking for an indefinitely large possible amount of partners

Beginner SQL - sum with JOIN possibly

I am relatively new to SQL and have only dealt with very basic statements before.
Table A
- id
- clubid
Table B
id
applicationid* (foreign key)id above ^
n
p
k
mg
area
What I am trying to do is as follows,
SELECT * from applications WHERE clubid = $id
Then with the result of this I need to run a query along the lines of,
SELECT Sum(n), Sum(p), Sum(k), Sum(mg) from productsapplied where area = $area
I know this second statement is wrong because it would be sum'ing each row which isnt possible so I am trying to find a way to link the two queries together. As I said I am a beginner with this, I have looked up on Sum as well as inner joins but can't get my head around it.
If more details are needed, let me know! Many thanks !
You need to add a GROUP BY clause to your query.
SELECT applications.id, applications.clubid, sum(products.n), sum(products.p)
FROM applications
JOIN productsapplied AS products
ON productsapplied.applicationid = application.id
GROUP BY application.id
WHERE clubid = $id AND products.area = $area;
Note that you will need to set GROUP BY appropriately. I am assuming that we are summing for each applicationid. I also didn't do every detail of each column as I assume you can figure that out on your own.
I think this may be what you're trying to do:
SELECT Sum(productsapplied.n), Sum(productsapplied.p), Sum(productsapplied.k), Sum(productsapplied.mg)
FROM productsapplied
JOIN applications
ON productsapplied.applicationid = applications.id
WHERE productsapplied.area = $area
AND applications.clubid = $id

SQL join or PHP loop? or what?

So i have two queries to give me the information i need and im trying to figure out the best way to get the result from them. i have a table of Holdings. using:
SELECT symbol, sum(shares) AS shares, sum(shares * price) AS cost
FROM Transactions
WHERE (action <>5)
AND date <= '2010-10-30'
GROUP BY symbol HAVING sum(shares) > 0
which results in
AGNC 50.00 1390.0000
RSO 1517.00 9981.8600
T 265.00 6668.7500
I then have another query
SELECT close FROM History WHERE symbol = $symbol AND date = $date
which will return the closing price of that day.
T 24.40
i want to basically for a given date calculate value so sum(shares * close) for each symbol. but i dont know how to do that with out looping through each row in php. i was hoping there was a join or something i could do in sql to make the data return the way i want it
I think you could do something similar to this: A select query selecting a select statement Put your second query literally in your first query. Not sure about exact syntax, but like:
SELECT
symbol,
sum(shares) AS shares,
sum(shares * price) AS cost,
sum(shares * close) as value
FROM Transactions
INNER JOIN History ON (symbol = $symbol AND date = $date)
WHERE (action <>5)
AND date <= '2010-10-30'
GROUP BY symbol HAVING sum(shares) > 0
#popnoodles is right about your naming though. If you use date I'd think you'd need [date].

Multiple Inner Joins and one to one // one to many relationship

I have this query below.. All of the relationships of the tables are one to one relationships except for ASSOCPRODUCTS table where there are two products per order. Everything seems to work fine, except for that my query returns only one row, and thus, will only return ONE product id when there are in fact two. I understand why it's only pulling one, in that there is only ONE order per orderID, but there are two Associated Products for each contract and I need to get each product id. in the Assocproducts table, each product gets its own row, as it is a one to many wit the contracts table.
Is it possible to get that information using inner joins, or do I need to run another query?
$orderid = $_POST['orderid'];
$res = mysql_query ("
SELECT company.name as cname,
orders.datemade as datemade
orders.p1quantity as p1q,
orders.p2quantity as p2q,
assocproducts.productid as pid,
assocproducts.price as pprice,
inventory.name as pname,
inventory.quantity as pquantity
FROM orders
INNER JOIN contracts ON (contracts.id = orders.contractid)
INNER JOIN company ON (contracts.companyid = company.id)
INNER JOIN assocproducts ON (contracts.id = assocproducts.contractid)
INNER JOIN inventory ON (assocproducts.productid = inventory.id)
WHERE orders.id = " . $orderid);
$order = mysql_fetch_assoc($res);
Please let me know if I need to give more information.
$order isn't in a loop, because I need to display ONLY the order info for this specific order. It's an AJAX trigger on click.
Thanks!
I think you're wrong stating that only one row is returned. I created a fresh schema with only the columns used in your query, populated it with sample data and got out all matching results. I think the problem is with your usage of mysql_fetch_assoc() function. Semantics of this function are clear:
Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows.
so my guess is you never call mysql_fetch_assoc() again.
Given semantics of INNER JOIN and your data constraints, your query will always produce 0 or 1 rows. If you don't know why - please read SQL JOIN types & behavior. So, you have two options to fetch order details and information about associated products:
Use two separate select statements - one for order details (always 1 row), second for associated products (2 rows). Iterate on results of the second query - you will get information about one product for each row fetched.
Keep your existing query, but change the way you retrieve data from it by iterating over all rows (note that order information will be present in all fetched rows and will be the same for all of them):
$firstRow = true;
while (($order_product = mysql_fetch_assoc($res)) !== FALSE) {
if ($firstRow) {
// do something with order info
// but don't repeat it for consecutive rows
$firstRow = false;
}
// do something with currently fetched associated product info
}
It's a matter of taste, but I'd go with the first option - it looks less hacky.

Categories