I have two tables: sales and productlist. Let's say I have these ff value for its table
This is the ff value of sales
id | pcode | qty_sold |
1 | 123 | 20 |
2 | 123 | 20 |
This is the ff value for productlist
id | pcode | pleft |
6 | 123 | 20 |
7 | 333 | 40 |
My problem is this: When I update 1 product from productlist it updates properly with the product I want to update, but other product data is turned to 0. The expected value when updating the productlist must be look like this:
id | pcode | pleft |
6 | 123 | 60 |
7 | 333 | 40 |
I have this code so far:
mysql_query("UPDATE productlist SET pleft=pleft+(SELECT SUM(qty_sold) ".
"FROM sales WHERE sales.pcode=productlist.pcode)");
But it displays like this:
id | pcode | pleft |
6 | 123 | 60 |
7 | 333 | 0 |
i guess you missed WHERE clause in your UPDATE statement, like:
UPDATE productlist
SET pleft=pleft+(SELECT SUM(qty_sold) FROM sales WHERE sales.pcode=productlist.pcode)
WHERE productlist.pcode = "your_id";
You are only using WHERE for your inner query.
You have to use WHERE for outer query for updating particular record.
UPDATE productlist
SET pleft=pleft+(SELECT SUM(qty_sold) FROM sales WHERE sales.pcode=productlist.pcode)
WHERE productlist.pcode = "id";
The reason you get '0' is, Your select returns '40' for first ID but no rows returned for second. so it is considered as null. Thats y you get '0' for second id. Try using l ifnull(select,0) function.
UPDATE productlist SET pleft=pleft+ifnull((SELECT SUM(qty_sold)
FROM sales WHERE sales.pcode=productlist.pcode),0)
Related
Products :
--------------------------------------------
| ID | Group | Name | Sold |
--------------------------------------------
| 1 | A | Dell | 0 |
--------------------------------------------
| 2 | A | Dell | 0 |
--------------------------------------------
| 3 | B | Dell | 1 |
--------------------------------------------
| 4 | B | Dell | 1 |
--------------------------------------------
| 5 | C | Dell | 0 |
--------------------------------------------
| 6 | C | Dell | 1 |
--------------------------------------------
Hi everyone, i have a table (products) stored in MySql with many records, for now i'm using this query SELECT * FROM products WHERE sold = 0, in results i get :
--------------------------------------------
| ID | Group | Name | Sold |
--------------------------------------------
| 1 | A | Dell | 0 |
--------------------------------------------
| 2 | A | Dell | 0 |
--------------------------------------------
| 5 | C | Dell | 0 |
--------------------------------------------
i want to get only one record from each group, so the results will be like :
--------------------------------------------
| ID | Group | Name | Sold |
--------------------------------------------
| 1 | A | Dell | 0 |
--------------------------------------------
| 5 | C | Dell | 0 |
--------------------------------------------
You could easily do this by using a distinct clause and removing the id column. If you want to keep the id column you need to specify how one would chose which id to keep.
select distinct
`group`
, name
, sold
from
products
where
sold = 0;
To keep the row with the smallest id (as your example shows) something along the lines of the example below would work.
select
id
, `group`
, name
, sold
from
products
where
sold = 0
and id = (
select
min(p.id)
from
products p
where
p.`group` = products.`group`
and p.sold = 0
);
First, change your field named Group to something like Group_Name. GROUP is a reserved keyword, and if it is not causing you problems now it probably will later.
Second, you should ask yourself what you are really after. The following query should generate your desired result. It adds an additional condition where the IDs that are returned are the lowest numbered ID in each group.
SELECT * FROM products
WHERE sold = 0
AND ID IN (SELECT MIN(ID) FROM products WHERE sold = 0 GROUP BY Group_Name)
Why do you want that, though? That is not a normal desired end state. You should ask yourself why you care about the ID. It looks like your goal is to figure out which products have not sold anything. In that case, I would recommend this instead:
SELECT DISTINCT Group_Name, Name
FROM products
WHERE sold = 0
ORDER BY Group_Name, Name
I found the solution by using the statement GROUP BY,
SELECT * FROM products WHERE sold = 0 GROUP BY group
in the results now, i get only one record for each group and the minimal id without adding any other statement, and in my real table i am using product_group instead of group because it's a reserved word.
Try this:
SELECT `ID`, `Group`, `Name`, `Sold` FROM products WHERE sold = 0 GROUP BY `Group`;
I have the following problem: I have a table import_data
The table is enriched something like this
| id | profile_id | sku | vendor | price | importRun |
| 1 | 39 | 123 | myVen | 2.0 | 1 |
| 2 | 39 | 456 | myVen | 2.0 | 1 |
| 3 | 39 | 123 | myVen | 3.0 | 2 |
What I need to get is an output of all elements, but only once. Every sku must be unique. To make it even worse, I need the newest data, if they are redundant.
My output should be like this:
| id | profile_id | sku | vendor | price | importRun |
| 2 | 39 | 456 | myVen | 2.0 | 1 |
| 3 | 39 | 123 | myVen | 3.0 | 2 |
Short Summary about the structure:
ID = PK
sku = a unique identifier for an article
importRun = Needed for comparision.
So, to explain it: I have an import-script, which reads a given CSV-file and imports all articles listed in it. I get this CSV-file in a regular period (once a week). I need to collect all data and save it, to create a price-evolution later on.
After every Import, I increment the number of importRun, so that no information is lost (remember, I can't use REPLACE INTO or INSERT IGNORE.
Now, when I export it, I need the newest Data, if a SKU occures multiple times. In this case, the SKU 123 is inserted 2 times on 2 different runs. That means, since my newest Run had the number 2, I need this tupel (and ignore the first one).
When I work with DISINCT, it would still output the same table, since they aren't distinct at all, because importRun differs.
I should be able to use GROUP BY, but I couldn't figure out which value will be taken, the first- or the last one? (importRun 1 or 2)
Update 1
Followed by the idea of #mitkosoft, I tried the following:
SELECT DISTINCT t1.*
FROM import_data t1
INNER JOIN import_profiles imp on t1.profile_id = imp.id
INNER JOIN (
SELECT DISTINCT sku, MAX(importRun) AS importRun
FROM import_data
GROUP BY sku ) t2
ON t1.sku = t2.sku
WHERE imp.creditornr = 73329
AND t1.vendor = 'rackmountit'
AND t1.importRun = t2.importRun
** Update 2 **
I added the complete Structures for all tables, which are relevant.
Import-Data:
|
Import-Profiles
But i still get duplicates :/
All you need to do is to determine MAX(importRun) for each sku:
SELECT
t1.*
FROM
import_data t1
INNER JOIN (
SELECT sku, MAX(importRun) AS importRun FROM import_data GROUP BY sku
) t2
ON t1.sku = t2.sku
AND t1.importRun = t2.importRun
Output is:
+----+------------+-----+--------+-------+-----------+
| id | profile_id | sku | vendor | price | importRun |
+----+------------+-----+--------+-------+-----------+
| 2 | 39 | 456 | myVen | 2.0 | 1 |
| 3 | 39 | 123 | myVen | 3.0 | 2 |
+----+------------+-----+--------+-------+-----------+
2 rows in set
Perhaps you could join on the same table where sku = sku
i have this problem with me on how to sum total value from two different table and then after getting its total i want to subtract it. for example i have table "rsales" and "sales" and i have these ff vlue below.
data from "rsales"
id | total | pcode |
1 | 100 | 2143 |
2 | 100 | 2143 |
3 | 50 | 2222 |
4 | 50 | 2222 |
data from "sales"
id | total | pcode |
7 | 100 | 2143 |
8 | 50 | 2222 |
my problem is this. i want to sum all "total" values from sales and sum "total"value from rsales group by pcode.and then after getting its sum i want to subtract it. my page must be something like this.
total pcode
| 100 | 2143 |
| 50 | 2222 |
i have this ff code but it doesnt wor for me
sql "select sum(rsales.total)- sum(sales.total) as t1 where pcode = rsales.pcode"
Use:
SELECT
SUM(r.total)-(
SELECT SUM(s.total)
FROM sales AS s WHERE r.pcode=s.pcode
) as total,
r.pcode
FROM rsales AS r
GROUP BY r.pcode;
Output:
+--+--+--+--+--+-
| total | pcode |
+--+--+--+--+--+-
| 100 | 2143 |
| 50 | 2222 |
+--+--+--+--+--+-
2 rows in set
Have you tried something like this?
SELECT
SUM(L.total) as lTotal, SUM(R.total) as rTotal
FROM
sales L
INNER JOIN rsales R
ON
R.pcode = L.pcode
GROUP BY L.pcode
If you get expected values from both tables you can easily add Additions and Subtruction in FROM clause.
There's no joins needed to do this. This solution works if some pcodes are only in one table:
select SUM(total), pcode from (
select sum(total) as total, pcode from rsales group by pcode
union all
select SUM(-total) as total, pcode from sales group by pcode) salesTables
group by pcode
I have two tables named sales and rsales
I have this ff value for table sales
id | pcode | total | discount |
2 | 33 | 100 | 20 |
3 | 33 | 100 | 20 |
and i have this ff value for table rsales
id | pcode | total | discount | sales_id |
4 | 33 | 100 | 20 | 1 |
5 | 33 | 100 | 20 | 2 |
6 | 33 | 100 | 20 | 3 |
My problem is that when I update all values from table sales, the table rsales must be update either if sales.id is equal to sales_id.
so for example if I have updated table sales with id = 2 and 3, the sales_id 2 and 3 from rsales must be updated either.
take note : only 2 and 3 because only that ids is found in table sales ids.
I have this ff codes so far to update table rsales. but the output is shown not as what I meant. it update all values.
mysql_query("UPDATE sales AS t1, rsales AS t2
SET t1.total = '$total_discount',
t2.total = '$total_discount',
t2.discount = '$tot'
WHERE t1.pcode = '$pcode'");
You should add to the WHERE class the common column between the tables.
UPDATE sales AS t1, rsales AS t2
SET
t1.total = '$total_discount',
t2.total = '$total_discount',
t2.discount = '$tot'
WHERE
t1.id = t2.sales_id AND
t1.pcode = '$pcode'
Note: I haven't tested it.
I have a table of Products that looks like so:
| id | Description | Price |
| 1 | dinglehopper | 2.99 |
| 2 | flux capacitor | 48.99 |
| 3 | thing1 | 48.99 |
And so on...
Then I have an OrderLineItem table which, as you can guess, links each item in an order to the product:
| id | productID | OrderID |
| 43 | 1 | 12 |
| 44 | 2 | 12 |
| 52 | 3 | 15 |
So, as you can see, order #12 contains a dinglehopper and flux capacitor. How can I get this information in a single query? I just want ALL the products associated with a given OrderID in the OrderLineItem table.
May be by
select p.description,p.id,o.irderId
from
`orderLineItem` o, `product` p
where
p.id = o.productId;
or
select p.description,p.id,o.irderId
from `orderLineItem` o
join `product` p
on p.id = o.productId;
LEFT JOIN :)
http://www.w3schools.com/sql/sql_join_left.asp
#Pete About "single" query part, you should make VIEW from this join, if really going to use a lot.