Getting count of insert/update rows from ON DUPLICATE KEY UPDATE - mysql

I have a statement that tries to insert a record and if it already exists, it simply updates the record.
INSERT INTO temptable (col1,col2,col3)
VALUES (1,2,3)
ON DUPLICATE KEY UPDATE col1=VALUES(col1), col2=VALUES(col2), col3=VALUES(col3);
The full statement has multiple inserts and I'm looking to count number of INSERTs against the UPDATEs. Can I do this with MySQL variables, I've yet to find a way to do this after searching.

From Mysql Docs
In the case of "INSERT ... ON DUPLICATE KEY UPDATE" queries, the return value will be 1 if an insert was performed, or 2 for an update of an existing row.
Use mysql_affected_rows() after your query, if INSERT was performed it will give you 1 and if UPDATE was performed it will give you 2.

I've accomplished what you're describing using a while loop so that each iteration creates a MySQL statement that affects one row. Within the loop, I run the mysql_affected_rows() and then increment a counter depending upon whether the value returned was a 0 or a 1. At the end of the loop, I echo both variables for viewing.
The complete wording from MySQL Docs regarding the mysql_affected_rows function is (notice there are 3 possible values returned - 0, 1, or 2):
For INSERT ... ON DUPLICATE KEY UPDATE statements, the affected-rows
value per row is 1 if the row is inserted as a new row, 2 if an
existing row is updated, and 0 if an existing row is set to its
current values. If you specify the CLIENT_FOUND_ROWS flag, the
affected-rows value is 1 (not 0) if an existing row is set to its
current values.
(Sidenote - I set $countUpdate and $countInsert and $countUpdateNoChange to 0 prior to the while loop):
Here's the code that I developed that works great for me:
while (conditions...) {
$sql = "INSERT INTO test_table (control_number, name) VALUES ('123', 'Bob')
ON DUPLICATE KEY UPDATE name = 'Bob'";
mysql_query($sql) OR die('Error: '. mysql_error());
$recordModType = mysql_affected_rows();
if ($recordModType == 0) {
$countUpdateNoChange++;
}elseif($recordModType == 1){
$countInsert++;
}elseif($recordModType == 2){
$countUpdate++;
};
};
echo $countInsert." rows inserted<br>";
echo $countUpdateNoChange." rows updated but no data affected<br>";
echo $countUpdate." rows updated with new data<br><br>";
Hopefully, I haven't made any typos as I've recreated it to share while removing my confidential data.
Hope this helps someone. Good luck coding!

I know this is a bit old, but I was doing a bulk insert in PHP and needed to know exactly how many rows were inserted and updated (separately).
So I used this:
$dataCount = count($arrData); // number of rows in the statement
$affected = mysql_affected_rows(); // mysqli_*, PDO's rowCount() or anything
$updated = $affected - $dataCount;
$inserted = 2 * $dataCount - $affected;
Simple trace table:
-------------------------------
| data | affected | ins | upd |
-------------------------------
| 1 | 1 | 1 | 0 |
-------------------------------
| 2 | 2 | 2 | 0 |
| 2 | 3 | 1 | 1 |
| 2 | 4 | 0 | 2 |
-------------------------------
| 3 | 3 | 3 | 0 |
| 3 | 4 | 2 | 1 |
| 3 | 5 | 1 | 2 |
| 3 | 6 | 0 | 3 |
-------------------------------
| 4 | 4 | 4 | 0 |
| 4 | 5 | 3 | 1 |
| 4 | 6 | 2 | 2 |
| 4 | 7 | 1 | 3 |
| 4 | 8 | 0 | 4 |
-------------------------------
| 5 | 5 | 5 | 0 |
| 5 | 6 | 4 | 1 |
| 5 | 7 | 3 | 2 |
| 5 | 8 | 2 | 3 |
| 5 | 9 | 1 | 4 |
| 5 | 10 | 0 | 5 |
-------------------------------

if you want to get the number of records that have been inserted and updated separetly, you are to issue each statement separetly.

Related

Making changes to multiple records based on change of single record with SQL

I have a table of food items. They have a "Position" field that represents the order they should appear in on a list (listID is the list they are on, we don't want to re-order items on another list).
+--id--+--listID--+---name---+--position--+
| 1 | 1 | cheese | 0 |
| 2 | 1 | chips | 1 |
| 3 | 1 | bacon | 2 |
| 4 | 1 | apples | 3 |
| 5 | 1 | pears | 4 |
| 6 | 1 | pie | 5 |
| 7 | 2 | carrots | 0 |
| 8,9+ | 3,4+ | ... | ... |
+------+----------+----------+------------+
I want to be able to say "Move Pears to before Chips" which involves setting the position of Pears to position 1, and then incrementing all the positions inbetween by 1. so that my resulting Table look like this...
+--id--+--listID--+---name---+--position--+
| 1 | 1 | cheese | 0 |
| 2 | 1 | chips | 2 |
| 3 | 1 | bacon | 3 |
| 4 | 1 | apples | 4 |
| 5 | 1 | pears | 1 |
| 6 | 1 | pie | 5 |
| 7 | 2 | carrots | 0 |
| 8,9+ | 3,4+ | ... | ... |
+------+----------+----------+------------+
So that all I need to do is SELECT name FROM mytable WHERE listID = 1 ORDER BY position and I'll get all my food in the right order.
Is it possible to do this with a single query? Keep in mind that a record might be moving up or down in the list, and that the table contains records for multiple lists, so we need to isolate the listID.
My knowledge of SQL is pretty limited so right now the only way I know of to do this is to SELECT id, position FROM mytable WHERE listID = 1 AND position BETWEEN 1 AND 5 then I can use Javascript (node.js) to change position 5 to 1, and increment all others +1. Then UPDATE all the records I just changed.
It's just that anytime I try to read up on SQL stuff everyone keeps saying to avoid multiple queries and avoid doing syncronous coding and stuff like that.
Thanks
This calls for a complex query that updates many records. But a small change to your data can change things so that it can be achieved with a simple query that modifies just one record.
UPDATE my_table set position = position*10;
In the old days, the BASIC programming language on many systems had line numbers, it encouraged spagetti code. Instead of functions many people wrote GOTO line_number. Real trouble arose if you numbered the lines sequentially and had to add or delete a few lines. How did people get around it? By increment lines by 10! That's what we are doing here.
So you want pears to be the second item?
UPDATE my_table set position = 15 WHERE listId=1 AND name = 'Pears'
Worried that eventually gaps between the items will disappear after multiple reordering? No fear just do
UPDATE my_table set position = position*10;
From time to time.
I do not think this can be conveniently done in less than two queries, which is OK, there should be as few queries as possible, but not at any cost. The two queries would be like (based on what you write yourself)
UPDATE mytable SET position = 1 WHERE listID = 1 AND name = 'pears';
UPDATE mytable SET position = position + 1 WHERE listID = 1 AND position BETWEEN 2 AND 4;
I've mostly figured out my problem. So I've decided to put an answer here incase anyone finds it helpful.
I can make use of a CASE statement in SQL. Also by using Javascript beforehand to build my SQL query I can change multiple records.
This builds my SQL query:
var sql;
var incrementDirection = (startPos > endPos)? 1 : -1;
sql = "UPDATE mytable SET position = CASE WHEN position = "+startPos+" THEN "+endPos;
for(var i=endPos; i!=startPos; i+=incrementDirection){
sql += " WHEN position = "+i+" THEN "+(i+incrementDirection);
}
sql += " ELSE position END WHERE listID = "+listID;
If I want to move Pears to before Chips. I can set:
startPos = 4;
endPos = 1;
listID = 1;
My code will produce an SQL statement that looks like:
UPDATE mytable
SET position = CASE
WHEN position = 4 THEN 1
WHEN position = 1 THEN 2
WHEN position = 2 THEN 3
WHEN position = 3 THEN 4
ELSE position
END
WHERE listID = 1
I run that code and my final table will look like:
+--id--+--listID--+---name---+--position--+
| 1 | 1 | cheese | 0 |
| 2 | 1 | chips | 2 |
| 3 | 1 | bacon | 3 |
| 4 | 1 | apples | 4 |
| 5 | 1 | pears | 1 |
| 6 | 1 | pie | 5 |
| 7 | 2 | carrots | 0 |
| 8,9+ | 3,4+ | ... | ... |
+------+----------+----------+------------+
After that, all I have to do is run SELECT name FROM mytable WHERE listID = 1 ORDER BY position and the output will be as follows::
cheese
pears
chips
bacon
apples
pie

Django: Saving to database connundrun

I have the following within my view which works perfectly:
if request.method == 'POST':
form = SelectTwoTeams(request.POST,user=request.user)
if form.is_valid():
teamSelection1 = UserSelection(campaignno = 102501350,
teamselection1or2 = 1,
teamselectionid_id = request.POST['team1'],
user_id=currentUserID,
fixturematchday=fixturematchday,
soccerseason_id=soccerseason)
teamSelection1.save()
teamSelection2 = UserSelection(campaignno = 102501350,
teamselection1or2 = 2,
teamselectionid_id = request.POST['team2'],
user_id=currentUserID,
fixturematchday=fixturematchday,
soccerseason_id=soccerseason)
teamSelection2.save()
When the code is run it ends up nicely in my database as it should:
mysql> select * from straightred_userselection;
+-----------------+------------+-------------------+-----------------+---------+-----------------+----------------+
| userselectionid | campaignno | teamselection1or2 | teamselectionid | user_id | fixturematchday | soccerseasonid |
+-----------------+------------+-------------------+-----------------+---------+-----------------+----------------+
| 9 | 102501350 | 1 | 6 | 349 | 2 | 1025 |
| 10 | 102501350 | 2 | 7 | 349 | 2 | 1025 |
+-----------------+------------+-------------------+-----------------+---------+-----------------+----------------+
2 rows in set (0.00 sec)
However, lets just say I now want to choose two different teams (currently 6 & 7 under the teamselectionid column). I can select them in my form and update but when the code is run it just adds another two rows to the table rather than update the current ones. The check to see if it needs to be a new set of team selections of an update is simply if:
fixturematchday and soccerseasonid are then same then update
fixturematchday and soccerseasonid are NOT the same then create new entries
As mention above it currently just adds two new rows as per below:
mysql> select * from straightred_userselection;
+-----------------+------------+-------------------+-----------------+---------+-----------------+----------------+
| userselectionid | campaignno | teamselection1or2 | teamselectionid | user_id | fixturematchday | soccerseasonid |
+-----------------+------------+-------------------+-----------------+---------+-----------------+----------------+
| 9 | 102501350 | 1 | 6 | 349 | 2 | 1025 |
| 10 | 102501350 | 2 | 7 | 349 | 2 | 1025 |
| 11 | 102501350 | 1 | 9 | 349 | 2 | 1025 |
| 12 | 102501350 | 2 | 14 | 349 | 2 | 1025 |
+-----------------+------------+-------------------+-----------------+---------+-----------------+----------------+
4 rows in set (0.00 sec)
Any help would be greatly appreciated, many thanks, Alan.
That's because you are creating new objects each time. Use update_or_create instead: https://docs.djangoproject.com/en/dev/ref/models/querysets/#update-or-create
Let me explain more about update_or_create, in your case, what you want is if the teamselectionid existed, just update that object.So should be
obj, created = UserSelection.objects.update_or_create(
teamselectionid=teamselectionid, defaults=what_field_you_want_to_update)
EDIT
The parameters before defaults just let you find the unique row in your database, For instance, a user may choose many teams, so only use user_id can't find the unique row, but you can combine user_id and team_id, like:
obj, created = UserSelection.objects.update_or_create(
user_id=user_id, team_id=team_id, defaults=what_field_you_want_to_update)

mysql (innodb) increment a value to create a hole

say I have a simple tree type table
id(key) | parent | order
============================
1 | 0 | 0
2 | 0 | 1
4 | 2 | 0
5 | 2 | 1
6 | 2 | 2
I want to insert a new node so that it has parent = 2 and order = 1, so it then the table data looks like:
id(key) | parent | order
============================
1 | 0 | 0
2 | 0 | 1
4 | 2 | 0
5 | 2 | 2
6 | 2 | 3
7 | 2 | 1
E.g. existing rows increment their order value.
What's the best way to ensure that existing rows increment their order (non-key field) starting at an existing arbitrary value, to make a hole for my insert statement?
If you have a new row with parent $p and order position $o you can:
UPDATE table SET order = order + 1 WHERE parent = $p AND order >= $o
and then:
INSERT INTO order (id,parent,order) VALUES($id,$p,$o)

MySQL only one active record, multiple inactive records

I've got this MySQL-table:
id | staffId | companyId | active | somevalue
When I create an UNIQUE index on staffId, companyId and active I can store a maximum of two records. One with active 1 and one with active 0.
I would like to make sure I can have only one active record while it is possible to have multiple inactive records. For example:
1 | 1 | 1 | 1 | 1
1 | 1 | 1 | 0 | 5
1 | 1 | 1 | 0 | 7
Should be possible, but adding this record should cause an error:
1 | 1 | 1 | 1 | 9
Choosing NULL instead of 0 for an incactive record seems to work :-) I wonder if there are better solutions?

Sort table records in special order

I have table:
+----+--------+----------+
| id | doc_id | next_req |
+----+--------+----------+
| 1 | 1 | 4 |
| 2 | 1 | 3 |
| 3 | 1 | 0 |
| 4 | 1 | 2 |
+----+--------+----------+
id - auto incerement primary key.
nex_req - represent an order of records. (next_req = id of record)
How can I build a SQL query get records in this order:
+----+--------+----------+
| id | doc_id | next_req |
+----+--------+----------+
| 1 | 1 | 4 |
| 4 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 1 | 0 |
+----+--------+----------+
Explains:
record1 with id=1 and next_req=4 means: next must be record4 with id=4 and next_req=2
record4 with id=5 and next_req=2 means: next must be record2 with id=2 and next_req=3
record2 with id=2 and next_req=3 means: next must be record3 with id=1 and next_req=0
record3 with id=3 and next_req=0: means that this is a last record
I need to store an order of records in table. It's important fo me.
If you can, change your table format. Rather than naming the next record, mark the records in order so you can use a natural SQL sort:
+----+--------+------+
| id | doc_id | sort |
+----+--------+------+
| 1 | 1 | 1 |
| 4 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 1 | 4 |
+----+--------+------+
Then you can even cluster-index on doc_id,sort for if you need to for performance issues. And honestly, if you need to re-order rows, it is not any more work than a linked-list like you were working with.
Am able to give you a solution in Oracle,
select id,doc_id,next_req from table2
start with id =
(select id from table2 where rowid=(select min(rowid) from table2))
connect by prior next_req=id
fiddle_demo
I'd suggest to modify your table and add another column OrderNumber, so eventually it would be easy to order by this column.
Though there may be problems with this approach:
1) You have existing table and need to set OrderNumber column values. I guess this part is easy. You can simply set initial zero values and add a CURSOR for example moving through your records and incrementing your order number value.
2) When new row appears in your table, you have to modify your OrderNumber, but here it depends on your particular situation. If you only need to add items to the end of the list then you can set your new value as MAX + 1. In another situation you may try writing TRIGGER on inserting new items and calling similar steps to point 1). This may cause very bad hit on performance, so you have to carefully investigate your architecture and maybe modify this unusual construction.