Insert row with cell amount dependent of previous row cell amount - php

I have a system balance table "A", where the column "amount" is a debit/credit column, like this:
id amount balance
1 100 100
2 -30 70
3 40 110
4 -20 90
If a new deposit is made of i.e. $50, the new row would have to be inserted as:
5 50 140
I could achieve this by selecting last balance:
SELECT id, balance
FROM A
ORDER BY id DESC LIMIT 1
and then inserting new row:
INSERT INTO A (amount, balance)
VALUES (50, previous_balance+50)
But, is there a way to achieve this with only one query? Would it be efficient on large databases? Thank you!

The correct way to do it is :
INSERT INTO A(amount,balance)
VALUES (50, 50 +
(SELECT B.balance
FROM(SELECT balance FROM A ORDER BY id DESC LIMIT 1) AS B)
);
Because mysql can't modify the same table which you use in the SELECT part

Just combine your 2 queries:
INSERT INTO A (amount, balance)
VALUES (50, (
SELECT balance
FROM (SELECT * from A)
ORDER BY id DESC LIMIT 1
)+50)

You can use this query:
INSERT INTO A (amount, balance)
SELECT "50", SUM(balance)
FROM ( SELECT id+1 as id, 50 balance FROM A
UNION ALL
SELECT id, balance FROM A
ORDER BY id DESC LIMIT 2
) t

Related

Split single row to multiple according to a number

Using the below select statement:
select
spec_sheet_color_comb.id,
spec_sheet_color_comb.pairs*2 as total
from
spec_sheet_color_comb
where
spec_sheet_color_comb.id_spec_sheet IN (4814)
And getting this result:
id total
79928 5
Now, I want to split this result according to the TOTAL quantity and get this result, 5 rows showing the ID:
Result
79928
79928
79928
79928
79928
It is really important, Thanks
Suppose you have a table having 2 columns id and total. (You can use your query instead of the table).
You need a collateral table (or view) with all the numbers 1..max_number_you_need
e.g.
(select 1 as number
union
select 2 as number
...
select 999 as number)
Then you can use the number source table joining your table you need to multiply rows
select id
from the_table t
join (table with numbers) n on n.number<=t.total
UPDATE: an example
SELECT id
from (select 1 as id, 5 as total
union
select 2 as id, 3 as total) t
join (select 1 as number
union
select 2 number
union
select 3 number
union
select 4 number
union
select 5 number) num on num.number<=t.total
order by id

Want to get lowest value from sql

In my database there are 5 values: 50, 75, 95, 125 and 200. When I query the minimum value I get 125, but I should get 50.
Here is my code:
$result_upTICKET = mysql_query("SELECT ID, EID, COMMISSION, MIN(PRICE) as PRICE FROM `tickets` WHERE EID='$EID_UPcoMing' AND STATUS='1'");
while($row_upTICKET = mysql_fetch_array($result_upTICKET))
{
$PRICE_upTICKET= $row_upTICKET['PRICE'];
$COMMISSION_upTICKET= $row_upTICKET['COMMISSION'];
}
What did I get wrong?
Try this one
SELECT ID, EID, COMMISSION, PRICE FROM `tickets` WHERE EID='$EID_UPcoMing' AND STATUS='1' ORDER BY PRICE ASC LIMIT 1
you are using where condition also. It filters rows first then apply/Select columns and min. I am sure that your where condition pull out row with price 50 and that's why min is coming as 125 instead of 50
Please execute your query like,We need to convert into interger
mysql_query("SELECT ID, EID, COMMISSION, MIN(CONVERT(PRICE, SIGNED INTEGER)) as PRICE FROM tickets WHERE EID='$EID_UPcoMing' AND STATUS='1'");

Fetching unique rows from one table that columns reference has not in another table

I've two table first Name orders
2nd named printing
orders table structure like this
id job_code job_name qty
1 597 xyz 1000
2 598 lmn 2500
3 599 oqr 20000
4 600 odc 15000
and printing table structure like this
id job_code dispatch qty
1 598 yes 1800
2 600 yes 1456
i want to select all job.code From orders which is not in printing table
I tried myself with this query.
SELECT DISTINCT orders.job_code, orders.job_name, orders.qty FROM orders
INNER JOIN printing
ON orders.job_code <> printing.job_code ORDER BY orders.job_code DESC LIMIT 10;
OR
SELECT DISTINCT orders.job_code, orders.job_name, orders.qty FROM orders
INNER JOIN printing
ON orders.job_code NOT IN (printing.job_code) ORDER BY orders.job_code DESC
LIMIT 10;
but it'll return all jobs which held on orders and printing tables
You can use this query.
SELECT
DISTINCT orders.job_code, orders.job_name, orders.qty
FROM
orders
WHERE
orders.job_code
NOT IN
(SELECT printing.job_code FROM printing)
ORDER BY
orders.job_code DESC
LIMIT 10
select job_code from orders
where job_cobe not in (select job_code from printing)
use the query like this
SELECT job_code FROM `orders` left join printingorders on orders.job_code not in ( select job_code from printingorders)
This will give you the result. I tried in my Phpmyadmin.

how to call last orders by order id from order table in database

Dear I need some help according to query from a table (database): such as I have a table "order_detail"
there are some fileds
order_id product_id product_name product_price product_quantity
3 4
3 5
4
5
6
Now I want to show data in thank-you page with all info of order_id 3 . How can I do that from model and controller ???
In layman terms:
(
SELECT salary
FROM tblName
ORDER BY salary DESC
LIMIT 1
OFFSET 1
)
UNION
(
SELECT salary
FROM tblName
ORDER BY salary ASC
LIMIT 1
OFFSET 1
)
For second max:
SELECT * FROM salary s ORDER BY s.value DESC LIMIT 1, 1
For second min:
SELECT * FROM salary s ORDER BY s.value ASC LIMIT 1, 1
For 2nd Maximum
SELECT salary_worth
FROM salary
WHERE salary_worth= (SELECT MAX(salary_worth) FROM salary WHERE salary_worth< (SELECT MAX(salary_worth) FROM salary))
For 2nd minimum
SELECT salary_worth
FROM salary
WHERE salary_worth= (SELECT MIN(salary_worth) FROM salary WHERE salary_worth> (SELECT MIN(salary_worth) FROM salary))
i know, that this is past question, but i came for next query to solve a problem:
SELECT * FROM (
SELECT #w:=#w+1 AS der,salary_worth
FROM salary, (SELECT #w:=0) AS del ORDER BY salary_worth) del2
WHERE der IN (2,#w-1);

select the 3 highest values using php mysql query

I have a table like this:
`id|value
1|2
2|8
3|5
4|6
5|10
6|7`
I need a query to pull AND sum the 3 highest values. So the correct query would pull the following:
3 highest:
5|10
2|8
6|7
Sum of 3 highest values = 25
I feel like this should be pretty simple but i'm having a tough time! Thanks for your help
SELECT SUM(Value) AS SumOfTop3Values
FROM (
SELECT Value
FROM Table
ORDER BY Value DESC
LIMIT 3
) AS sub
I think you need to wrap this in a subquery:
SELECT SUM(value) AS total FROM (
SELECT value FROM table
ORDER BY value DESC
LIMIT 3
);
To have MySQL return highest 3 values and their Sum in a 4th row, you can use (aasuming that id is the Primary Key of the table):
SELECT id, SUM(value)
FROM
( SELECT id, value
FROM TableX
ORDER BY value DESC
LIMIT 3
) AS tmp
GROUP BY id
WITH ROLLUP ;

Categories