mysql sum columns from multiple tables - php

I have two tables: tb_subjoe and tb_deliverydetails
I need to sum Price and Quantity column. at this my code mysql When I do
SELECT scode_id, scode_detail, deli_quote, SUM(deli_dequan) AS QUANTITY, SUM(deli_goodsprice) AS Good price, SUM(deli_wagesprice) AS Wage price
FROM tb_deliverydetails
LEFT JOIN tb_subjob
ON tb_subjob.scode_id = tb_deliverydetails.scode_id
GROUP BY `deli_quote`
it not answer for me

Related

How to create new table for the output of DISTINCT COUNT to be distributed in rows, not in column?

My query displays the DISTINCT count of buyers with corresponding ticketserial#. I need to automatically calculate the SOLD and BALANCE column and save into the database either into the existing table (table1) with the rows that corresponds to the ticketserial. I've already exhausted my brain and did google many times but I just can't figure it out. So I tried another option by trying to create a new table into the database for the output of DISTINCT COUNT but I didn't find any sample query to follow, so that I could just use INNER JOIN for that new table with table1, with that the PRINTED, SOLD are in the same table, thus I can subtract these columns to obtain the values for the BALANCE column.
Existing table1 & table2 are records in the database via html form:
Table1
Ticket Serial Printed Copies SOLD(sold) Balance
TS#1234 50 ?(should be auto ?
TS#5678 80 ?(should be auto ?
(so on and so forth...)
Table2
Buyer Ticket Serial
Adam TS#1234
Kathy TS#1234
Sam TS#5678
(so on and so forth...)
The COUNT DISTINCT outputs the qty. of sold tickets:
<td> <?php print '<div align="center">'.$row['COUNT(ticketserial)'];?></td>
...
$query = "SELECT *, COUNT(ticketserial) FROM buyers WHERE ticketsold != 'blank' GROUP BY
ticketserial ";
It's COUNT output looks like this:
Ticket Serial------Distinct Count
TS#1234 7
TS#5678 25
(so on and so forth...)
I tried to update the SOLD column and BALANCE column by UPDATE or INSERT and foreach loop but only the first row in table was updated.
Table1
Ticket Serial Printed Copies Sold Balance
TS#1234 50 **7** 0
TS#5678 80 **0** 0
TS#8911 40 **0** 0
(so on and so forth...)
Note: The fieldname "sold" in table1 is not the same with the fieldname "ticketsold" in table2 as the former is quantity and the later is ticketserials.
Your question is a bit hard to follow. However this looks like a left join on a aggregate query:
select
t1.ticket_serial,
t1.printed_copies,
coalesce(t2.sold, 0) sold,
t1.printed_copies - coalesce(t2.sold, 0) balance
from table1 t1
left join (
select ticket_serial, count(*) sold
from table2
group by ticket_serial
) t2 on t2.ticket_serial = t1.ticket_serial
If you are looking to update the main table:
update table1 t1
left join (
select ticket_serial, count(*) sold
from table2
group by ticket_serial
) t2 on t2.ticket_serial = t1.ticket_serial
set
t1.sold = coalesce(t2.sold, 0),
t1.balance = t1.printed_copies - coalesce(t2.sold, 0)
I would not actually recommend storing the sold and balance in the main table - this is derived information that can be easily computed when needed, and would be tedious to maintain. If needed, you could create a view using the first above SQL statement, which will give you an always up-to-date perspective at your data.

Need SQL query with good performance to select data that does NOT match criteria

I have a database with
a company table
a country table
a company_country n:n table which defines which company is available in which country
a product table (each product belongs to one specific categoryId)
and a company_product_country n:n:n table that defines which company offers which product in which country.
The latter has the three primary key columns companyId, productId, countryId and the additional columns val and limitedAvailability. val is an ENUM with the values yes|no|n/a, and limitedAvailability is an ENUM with the values 0|1.
Products within categories 1 or 2 are available in all countries and therefore get countryId = 0. But at the same time, only these very products may have a limitedAvailability = 1.
An SQLFiddle with a test database can be found here: http://www.sqlfiddle.com/#!9/a065a/1/0
It contains five countries, products and companies.
Background information on what I need to select from the database:
A PHP script generates a search form where an arbitrary list of countries and products can be selected. The products are separated by categories (I did not add the category table in the sample database, because it is not needed in this case). For the first category, I can select whether to exclude products with limited availability.
Generating the desired result works fine:
It displays all companies that are available in the selected countries and have at least one of the selected products available. The result offers a column that defines how many of the selected products are available by company.
If the user defines that one or more categories should not contain products with limited availability, then the products within the corresponding categories will not count as a match if the company offers them with limited availability only.
I am pleased with the performance of this query. My original database has got around 15 countries, 100 companies and 150 products. Selecting everything in the search form occupies the MySQL server for around two seconds which is acceptable for me.
The problem:
After generating the result list of companies which matches as many product search criteria as possible, I use PHP to iterate through those companies and run another SQL query that should give me the list of products that the company does not offer corresponding to the search criteria. The following is an example query for companyId 1 to find out which products are not available when
the desired products have the productIds 2, 4 and 5
the product's country availability should be at least one of the countryIds 1, 2 or 3
the product should not have a limitedAvailability when it is from categoryId = 2:
SELECT DISTINCT p.name
FROM `product` p
LEFT JOIN `company_product_country` cpc ON `p`.`productId` = `cpc`.`productId` AND `cpc`.`companyId` = 1
WHERE NOT EXISTS(
SELECT *
FROM company_product_country cpcTmp
WHERE `cpcTmp`.`companyId` = 1
AND cpcTmp.val = 'yes'
AND (
cpcTmp.limitedAvailability = 0
OR p.categoryId NOT IN(2)
)
AND cpcTmp.productId = p.productId
)
AND p.`productId` IN (2,4,5)
AND countryId IN(0,1,2,3);
The database along with this query can be found on the SQLFiddle linked above.
The query generates the correct result, but its performance dramatically decreases with the number of products. My local SQL server needs about 4 seconds per company when searching for 150 products in 15 countries. This is inaccpetable when iterating through 100 companies. Is there any way to improve this query, like avoiding the IN(...) function containing up to 150 products? Or should I maybe split the query into two like so:
First fetch the unmatched products that do not have country Id 0 and are IN the desired countryIds
Then fetch the unmatched products in countryId = 0 and if applicable filter limitedAvailability = 0
?
Your help is gladly appreciated!
I would suggest writing the query like this:
SELECT p.name
FROM product p
WHERE EXISTS (select 1
from company_product_country cpc
where p.productid = cpc.productid and
cpc.companyid = 1 and
cpc.countryid in (1, 2, 3)
) and
NOT EXISTS (select 1
from company_product_country cpcTmp
where cpcTmp.productId = p.productId and
cpcTmp.companyId = 1 and
cpcTmp.val = 'yes' and
cpcTmp.limitedAvailability = 0
) AND
NOT EXISTS (select 1
from company_product_country cpcTmp
where cpcTmp.productId = p.productId and
cpcTmp.companyId = 1 and
cpcTmp.val = 'yes' and
p.categoryId NOT IN (2)
)
p.`productId` IN (2, 4, 5) ;
Then, you want the following indexes:
product(productid, categoryid, name)
company_product_country(productid, companyid, countryid)
company_product_country(productid, companyid, val, limitedavailability)
company_product_country(productid, companyid, val, category)
Note: these indexes completely "cover" the query, meaning that all columns in the query come from the indexes. For most purposes, is probably sufficient to have a single index on company_product_country. Any of the three would do.
Take the query that identifies the products that match the user selection. Subquery it and outer join it to the products table. Exclude the matches.
SQL Fiddle
SELECT p.name
FROM
product p LEFT JOIN
(
SELECT productId
FROM company_product_country cpcTmp
WHERE companyId = 1 AND
countryId IN (0,1,2,3) AND
(
productId IN (4, 5) OR
(productId = 2 AND limitedAvailability = 0)
)
) t
ON p.productId = t.productId
WHERE
t.productId IS NULL AND
p.productId IN (2,4,5)

MySQL query printing double records

All recorders in sales & purchase table are only entered Once. I have checked it carefully. However I wish to combine these two tables in such a way that both tables will be completely fetched. here is my query
note that timestamp column has mysql DATE format
Select
sales.ID as sid,
sales.saleHatsh,
sales.timestamp as sdate,
sales.gatePass as sGP,
sales.pname as sPN,
sales.description as sDES,
sales.balance as sbal,
purchase.ID as pid,
purchase.purchaseHatsh,
purchase.timestamp as pdate,
purchase.gatePass as pGP,
purchase.pname as pPN,
purchase.description as pDES,
purchase.balance as pbal
from sales,purchase
where sales.timestamp='2013-11-11' OR purchase.timestamp='2013-11-11'
here is the result of my query & sales & purchase table
Sales table only have 2 recorder
Purchase table only has 4 recorder
What is happening there is that you are not joining those tables in any way. so you are getting all the possible matches from those tables. Looking at the columns i don't think you want to JOIN, but probably you want a UNION instead:
SELECT
sales.ID AS id,
sales.saleHatsh AS hatsch,
sales.TIMESTAMP AS date,
sales.gatePass AS GatePass,
sales.pname AS pname,
sales.description AS Description,
sales.balance AS balance,
'SALE' AS transanctionType
FROM sales
WHERE sales.TIMESTAMP = '2013-11-11'
UNION
SELECT
purchase.ID,
purchase.purchaseHatsh,
purchase.TIMESTAMP,
purchase.gatePass,
purchase.pname,
purchase.description,
purchase.balance,
'PURCHASE'
FROM purchase
WHERE purchase.TIMESTAMP = '2013-11-11'
I added a column "transactionType" for you to identify which ones are sales or purchases.

Subtract columns from two tables after grouping (for inventory)

So for my inventory system I have two tables with the same columns names (one for stock produced and one for stock shipped). I figured out how to group the columns by the product and then sum the quantities. So I want to run this query on both tables then subtract the quantity column from each table where the product variables match up.
I use this to add group and sum stock totals (in):
$query = "SELECT id, type, color, product, SUM(Quantity) AS TotalQuantity FROM inventory GROUP BY id, color, type";
And I use this to group and sum stock shipments (out):
$query = "SELECT id, type, color, product, SUM(Quantity) AS TotalQuantity FROM shipped GROUP BY id, color, type";
So how do I subtract the quantity columns for each of these?
Edit:
I use this for output: (a table)
echo '<tr><td>'. $row['product'] . '</td><td id="replace">' . $row['type'] . '</td><td>' . $row['color']. '</td><td>'. $row['TotalQuantity'];
echo "</td></tr>";
This can be done entirely in one query. An INNER JOIN between these will allow you to subtract the quantities. The id, color, product columns are only needed from one of the tables in your SELECT list.
SELECT
inv.id,
inv.color,
inv.product,
/* total inventory quantity */
SUM(inv.Quantity) AS TotalInvQuantity,
/* total shipped quantity */
SUM(ship.Quantity) AS TotalShipQuantity,
/* inventory quantity minus shipped quantity */
SUM(inv.Quantity) - COALESCE(SUM(ship.Quantity), 0) AS SubtractedQuantity
FROM
inventory inv
LEFT JOIN shipped ship ON inv.id = ship.id AND inv.color = ship.color AND inv.product = ship.product
GROUP BY
inv.id,
inv.color,
inv.product
Update after comments
SELECT
inv.id,
inv.color,
inv.product,
inv.TotalInvQuantity,
COALESCE(ship.TotalShipQuantity, 0) AS TotalShipQuantity,
inv.TotalQuantity - COALESCE(ship.TotalQuantity, 0) AS SubtractedQuantity
FROM (
SELECT id, product, color, SUM(Quantity) AS TotalInvQuantity
FROM inventory
GROUP BY id, product, color
) inv
LEFT JOIN (
SELECT id, product, color, SUM(Quantity) AS TotalShipQuantity
FROM inventory
GROUP BY id, product, color
) ship ON
inv.id = ship.id
AND inv.product = ship.product
AND inv.color = ship.color

joining tables in mySQL to get a particular column

I have two tables :
- cart , cols are (painting_id, session_id, ip, user_agent)
- paintings , cols (painting_id, price)
Now I have to select the painting id from the table cart. I need to Join the two tables and get the sum of the price of all the paintings from table 'paintings'. Note the table cart doesnt have the price column , it has to be imported from the 'paintings' table. Only the sum of price of those paintings are shown which has been added into the cart table by a particular session id or email id.
Here is the query i have tried so far
SELECT p.SUM(price) FROM paintings
p JOIN cart c ON p.painting_id = c.painting_id
WHERE c.session_id = '$session'
It should be SUM(p.price) instead.
Try this query
SELECT cart.user_agent, sum(paintings.price)
from cart inner join
paintings on
cart.painting_id=paintings.painting_id
where session_id='$session'
if you want the total cost of all the users
SELECT cart.user_agent, sum(paintings.price)
from cart inner join
paintings on
cart.painting_id=paintings.painting_id
group by cart.session_id

Categories