Can you re-use update values for on duplicate key updates? - mysql

So let's say you have column A,B,C. On duplicate key, let's say you do A = {some statement here}, B = {some statement here}, and C= New_A + New_B. Can I use what would be the new values of A and B in order to determine the new value of C, or do I have to retype the expressions for the new A and B? Thanks!

I think you can do it. If you do:
ON DUPLICATE KEY UPDATE
A = A + 1, B = B * 2,
C = A + B
I believe the updates are executed left to right. So when it gets to C = A + B, A and B contain the new values.

Related

MySQL "INSERT INTO SELECT NOT EXISTS" Efficiency

The following bit of code is causing me no end of headaches, it works; but it is taking upwards of 30min with only 16k rows; the final database will be looking to include 1-5M rows.
So I'm looking to make things more efficient
INSERT INTO r (aKey, bKey, c, d, e, fKey, g, h, i, j, k, l, mKey, nKey, o, pKey, qKey, r, sKey)
SELECT aKey, bKey, c, d, e, fKey, g, h, i, j, k, l, mKey, nKey, o, pKey, qKey, r, sKey
FROM t2
WHERE NOT EXISTS
(
SELECT i,g
FROM r
WHERE t2.i = r.i AND t2.g = r.g
);
The field of i & g are the uniques (not specified in the DB currently - should they be? but a requirement)
Anything with Key on is a foreign key referencing another table
After the above statement I then loop through each none unique and update if it is different.
UPDATE r
INNER JOIN t2 ON t2.i = r.i
SET r.d=t2.d;
I have looked into :
INSERT ... ON DUPLICATE KEY UPDATE
but I was never able to get it to work; as all the examples I found were not dealing with a insert select
Any suggestions to improve the SQL will be most helpful.
Conclusion was: Due to the size of the request best to go back to basics and "set unique database fields to UNIQUE" and then do a INSERT IGNORE
Code below: (including python variable inserts)
INSERT IGNORE INTO """ + tblName + """ (""" + dupeString + """)
SELECT """ + dupeString + """
FROM t2
ON DUPLICATE KEY UPDATE """ + updateString + """;
dropped from 30-40m query to... less than 3 seconds.

Mysql, change (update) all numeric values for all table

source:
constant x,y,
mysql table named 'table1'. It contains column pp.
Need change all values in table1:
pp_new = pp/x*y;
pp_new = round(pp_new) to 0.1.
For example 12.31 = 12.30, 14.56 = 14.50, 55.1245035 = 55.10
12.31 = 12.30, 14.56 = 14.50, 55.1245035 = 55.10
You need to use truncate(number,decimalplaces)
TRUNCATE(1.25864,1)
this will return 1.2
What about something like UPDATE table1 SET pp = round(pp/x*y) ?

Update table using alias

I need to fill some fields in a table getting informations from other records of the same table.
I tried to write a query to explain what I want to do:
update globale2
set
nita = t.nita,
tita = t.tita,
notaita = t.notaita
where
neng = t.neng and
nita is null
(select nita, neng, tita, notaita from globale where uris='mma' and nita is not null) as t
edit to eplain better:
every records have these fields: "nita", "tita", "notaita", "neng" ("neng" cannot be null)
I want to fill these fields: "nita", "tita", "notaita" (where "nita" is empty)
with the same values from another record where "neng" equals the other "neng"
You can however, join the two tables.
UPDATE globale2 g
INNER JOIN globale gg
ON g.neng = gg.neng
SET g.nita = gg.nita,
g.tita = gg.tita,
g.notaita = gg.notaita
WHERE g.nita IS NULL
AND gg.uris = 'mma'
AND gg.nita IS NOT NULL
assume there is a table A_temp, with two columns 'one' and 'two'.
TABLE A_temp
ONE TWO
1 2
this is the present status of the table.
The query
UPDATE (SELECT * FROM A_temp ) A SET one = A.two where one = '1'
updates the table as
ONE TWO
2 2
Hope you get the idea and that it helps..

Join with on duplicate not working as expected

I'm trying to run a query that looks like this:
$DB->query("INSERT INTO table (
SELECT s.value1, s.value2, s.value3, t.value1, u.value1, u.value2, u.value3, u.value4
FROM aTable AS s
LEFT JOIN aTable2 AS u ON u.uID = s.uid
WHERE u.spent < ". $needed ."
GROUP BY s.uid, s.fid )
ON DUPLICATE KEY UPDATE
value1 = VALUES(u.value1),
value2 = VALUES(u.value2),
value3 = VALUES(u.value3),
value4 = VALUES(u.value4);
");
However this returns the error:
Unknown column 'u.value1' in 'field list'
Please note that this is the first time I'm working with MySQL at this level, so I have no idea how the DUPLICATE KEY UPDATE function works...
What i intent to do with the table is like a cache, because the infromation is fetched from some very large table
The values statement refers to the values in the column list for the insert. You don't have these listed, but a guess is:
ON DUPLICATE KEY UPDATE
value1 = VALUES(value1),
value2 = VALUES(value2),
value3 = VALUES(value3),
value4 = VALUES(value4);

Group By and Sum clauses in LINQ

I've written a simple linq query as follows:
var query = from c in context.ViewDeliveryClientActualStatus
join b in context.Booking on c.Booking equals b.Id
join bg in context.BookingGoods on c.Booking equals bg.BookingId
select new { c, b, bg };
I have filtered the previous query with a number of premises and then needed to group by a set of fields and get the sum of some of them, as so:
var rows = from a in query
group a by new {h = a.c.BookingRefex, b = a.c.ClientRefex, c = a.b.PickupCity, d = a.b.PickupPostalCode} into g
select new
{
Booking_refex = g.Key.h,
Client_refex = g.Key.b,
//Local = g.
Sum_Quan = g.Sum(p => p.bg.Quantity),
};
I'd like to get a few values from a which I haven't included in the group by clause. How can I get those values? They're not accessible through g.
The g in your LINQ expression is an IEnumerable containing a's with an extra property Key. If you want to access fields of a that are not part of Key you will have to perform some sort of aggregation or selection. If you know that a particular field is the same for all elements in the group you can pick the value of the field from the first element in the group. In this example I assume that c has a field named Value:
var rows = from a in query
group a by new {
h = a.c.BookingRefex,
b = a.c.ClientRefex,
c = a.b.PickupCity,
d = a.b.PickupPostalCode
} into g
select new {
BookingRefex = g.Key.h,
ClientRefex = g.Key.b,
SumQuantity = g.Sum(p => p.bg.Quantity),
Value = g.First().c.Value
};
However, if c.Value is the same within a group you might as well include it in the grouping and access it using g.Key.cValue.
Just add those field in the
new {h = a.c.BookingRefex, b = a.c.ClientRefex, c = a.b.PickupCity, d = a.b.PickupPostalCode}
they will be accessible in g then.