I need to batch update balances of some users in members table, how should mysql query be?
MembersArray = array(1,3,4);
AmountToBeAdded = 10;
members
id. balance
1. 45
2. 15
3. 0
4. 120
5. 80
Thanks already for your help
UPDATE members SET balance = balance + 10
WHERE members.id IN(1,3,4)
Related
I'm creating a Queuing system, with mysql and vb.net that prints ticket no.
Scenario:
Ticket will proceed to billing (2 windows), then after that proceed to cashier (2 windows) when ticket is called on dashboard, then after cashier - ticket will be closed.
My tbl_ticket
I have here my MySql query that assign a ticket to a cashier window. this works for a single cashier user. but I have 2 cashier.
Cashier 1 = (#agentID = 1) and Cashier 2 = (#agentID = 2)
UPDATE tbl_ticket, (
SELECT * FROM tbl_ticket WHERE current_agent = 0 AND cashier_stat = 1 AND DATE(tag_time) = DATE(NOW()) ORDER BY tag_time ASC LIMIT 1 FOR UPDATE
) as ticket
SET tbl_ticket.current_agent = #agentID
WHERE tbl_ticket.id = ticket.id;
I update tbl_ticket with agentid on tbl_ticket.current_agent for assigning what ticket to assess for Cashier 1 or Cashier 2.
Does this query can ensure that if two cashier execute this in the same time they will not be assigned in the same number in sequence? I want 2 cashier to get different ticket at the same time.
I have two (2) tables:
ps_wk_mp_seller_transaction_history
And ps_wk_mp_seller_order_status
I want to totalize seller_amount only if current_state = 5
For that I have written this:
select sum(seller_amount) as seller_amount
from ps_wk_mp_seller_transaction_history th inner join
ps_wk_mp_seller_order_status os
on os.id_order = th.id_transaction and
os.current_state = 5 and
th.id_customer_seller = 2;
For id_customer_seller=2 I get this 4984.020000 (4950+34.02) and it is exact
But I get 25.848000 instead of NULL for id_customer_seller=5
Can someone help me?
May be you can test yourself, this the code: https://github.com/kulturman/fakerepo
First of all your records have some logic issue, id_transaction have tow transactions with same id_transaction but different amount and different state ! so transaction with id 41 having state 5 and that why customer 5 will not be null because 41 is in state 5.
To fix this
transaction id must be unique id for every transaction in order to differentiate between transaction state and amount
the query should be like this
select sum(seller_amount) as seller_amount
from ps_wk_mp_seller_transaction_history th
left join ps_wk_mp_seller_order_status os
on os.id_order=th.id_transaction
where
th.id_customer_seller = 2
and os.current_state=5
working example here
MySQL Table:
Key Total
A 20
B 15
C 25
D 30
I have this data:
Key Total
A 5
C 10
D 10
I want to update the table:
by adding each row's total. Note the data is incomplete, so some rows (B) are not going to be updated. So the result should be:
Key Total
A 25
B 15
C 35
D 40
My current plan is running update table set total = total + ? where key = ?. But is there a more efficient way?
If the second set of data were stored in a table then it would have been much easier.
Let's say your first table is named as first_table and your second data set is stored a table named second_table.
Query:
UPDATE first_table AS FT
INNER JOIN second_table AS ST
ON FT.key = ST.key
SET FT.total = FT.total + ST.total;
SEE DEMO
lets say I have the following table
players
player_id score
1 - 300
2 - 400
can I do a query that will add 10 to the score of a player?
because right now all I can think of is query the db, get the score, add 10 to it, then query the db and replace 300 with 310?
PS: Im new to SQL
You may try this:-
UPDATE players
SET score = score + 10
WHERE player_id = 1
Also check UPDATE in MySQL
UPDATE players SET score = score + 10 WHERE player_id = 1
I have this SQL:
UPDATE products pr
SET pr.product_model_id = (SELECT id FROM product_models pm WHERE pm.category_id = 1 ORDER BY rand() LIMIT 1)
limit 200;
It took the mysql-server more then 15 seconds for these 200 records. and in the tablem there are 220,000 records.
why is that?
edit:
I have these tables which are empty, but I need to fill them with random information for testing.
True estimations shows that I will have:
80 categories
40,000 models
And, around 500,000 products
So, I've manually created:
ALL the categories.
200 models (and used sql to duplicate them to 20k).
200 products (and duplicated them to 250k)
I need them all attached.
DB tables are:
categories {id, ...}
product_models {id, category_id, ...}
products {id, product_model_id, category_id}
Although question seems to be little odd but here is a quick thought about the problem.
RAND function doesn't perform well on large data-set.
In mysql, developer try to achieve this in different ways, check these posts:
How can i optimize MySQL's ORDER BY RAND() function?
http://www.titov.net/2005/09/21/do-not-use-order-by-rand-or-how-to-get-random-rows-from-table/
One of the quick way is following(in php):
//get the total number of row
$result= mysql_query("SELECT count(*) as count
FROM product_models pm WHERE pm.category_id = 1 ");
$row = mysql_fetch_array($result);
$total=$row['count'];
//create random value from 1 to the total of rows
$randomvalue =rand(1,$total);
//get the random row
$result= mysql_query("UPDATE products pr
SET pr.product_model_id =
(SELECT id FROM product_models pm
WHERE pm.category_id = 1
LIMIT $randomvalue,1)
limit 200");
Hope this will help.
The 'problem' is the ORDER BY rand()