Access: Search and Update Query - mysql

I'm running a query in access to search a table with two fields of numbers. One field is "Dose" and the other is "Volume". Below I'm searching for the "Dose" when the "Volume" is equal to a value nearest to 2 and dividing by 100 to get the correct units, which works fine.
SELECT TOP 1 [Table1]![Dose]/100 AS CentiDose
FROM [Table1]
ORDER BY Abs([Table1]![Volume]-2);
However I want to use UPDATE (or at least that's what I think?) to take this value and insert it into a field of one record in another table ie Table2, record 1, field 1. Is this possible? Any help would be greatly appreciated as I'm a novice at using Access.

If you're looking to update an existing row in table2 then UPDATE is what you want, something like
UPDATE Table2
SET Field1 = Centidose
WHERE Table2.recordid = x
If you need to append a new record to table2, it would look more like
INSERT INTO Table2
SET Field1 = Centidose
WHERE Table2.recordid = x

Related

MySQL - UPDATE <table> SET (n<values>) WHERE <table>.`foreign_key`

Just to get the elephant out of the room since there are so many SO questions similar to this. I have looked through the other posts with similar names and did not find what I was looking for. That being said I may have just not seen how to implement their question to my specific implementation
I am trying to update multiple rows in a table to all [potentially] different values but I don't know how many rows I will be updating as I am targeting using a foreign key. However, due to database constraints it is safe to assume that I have the correct number of inputs. (e.g. 3 values for 3 rows, or 5 values for 5 rows) AND even if it was not safe to assume, I don't mind an error being thrown as that would insure data integrity if something did happen to be wrong...
It does not matter which values get assigned to which rows.
Here is some examples of what I have tried.
UPDATE table1
SET
table1.column1 IN (17 , 37, 62)
WHERE
table1.foreign_key = 877;
After that I found out you can't use IN like that...
Then I tried:
UPDATE table1
SET
table1.column1 = (17 , 37, 62)
WHERE
table1.primary_key IN (SELECT
primary_key
FROM
table1
WHERE
foreign_key = 877);
However, "You can't specify target table 'table1' for update in FROM clause"
This would be fairly trivial to do with a SELECT then an update after (n_rows + 1 queries) (shown below), so I am assuming it is possible to do in one query.
SELECT primary_key from table1 where foreign_key = ?;,[877]
Then mapping the values and primary keys (I am using nodejs backend)
UPDATE table1 set column1 = ? where primary_key = ?;,[value1,primary_key1]
The hard parts seem to be
How to map multiple values to multiple rows.
How to get the unique IDs for each row at run time.
I don't mind if I have to use a stored procedure, I just really don't want to send n+1 queries to the database.
you need to update your datas with just one query like this below.
insert into TABLENAME(key, column1, column2, ...) values (?),(?),... on duplicate key update column1=value(column1), column2=value(column2), ...;
also you can visit my post with this link.
Node.js Update Table with Array in Mysql
I had same problem 2 days ago.

update sql table current row

Complete noob alert! I need to store a largish set of data fields (480) for each of many devices i am measuring. Each field is a Decimal(8,5). First, is this an unreasonably large table? I have no experience really, so if it is unmanageable, I might start thinking of an alternative storage method.
Right now, I am creating a new row using INSERT, then trying to put the 480 data values in to the new row using UPDATE (in a loop). Currently each UPDATE is overwriting the entire column. How do I specify only to modify the last row? For example, with a table ("magnitude") having columns "id", "field1", "field2",...:
sql UPDATE magnitude SET field1 = 3.14; this modifies the entire "field1" column.
Was trying to do something like:
sql UPDATE magnitude SET field1 = 3.14 WHERE id = MAX(id)
Obviously I am a complete noob. Just trying to get this one thing working and move on... Did look around a lot but can't find a solution. Any help appreciated.
Instead of inserting a row and then updating it with values, you should insert an entire row, with populated values, at once, using the insert command.
I.e.
insert into tTable (column1, column2, ..., column n) values (datum1, datum2, ..., datum n)
Your table's definition should have the ID column with property identity, which means that it will autofill it for you when you insert, i.e. you don't need to specify it.
Re: appropriateness of the schema, I think 480 is a large number of columns. However, this is a straightforward enough example that you could try it and determine empirically if your system is able to give you the performance you need.
If I were doing this myself, I would go for a different solution that has many rows instead of many columns:
Create a table tDevice (ID int, Name nvarchar)
Create a table tData (ID int, Device_ID int, Value decimal(8,5))
-- With a foreign key on Device_ID back to tDevice.ID
Then, to populate:
Insert all your devices in tDevice
Insert one row into tData for every Device / Data combination
-- i.e. 480 x n rows, n being the number of devices
Then, you can query the data you want like so:
select * from tData join tDevice on tDevice.ID = tData.Device_ID

How to update the nth row in a SQL database table?

How can I update only the nth row from a table?
To update the second row for example, i tried using UPDATE and LIMIT, but it is giving me an error.
UPDATE database_table
SET field = 'new_value'
LIMIT 0, 1
Is there a way to do it?
If you have a primary key and a column you'd like to order by (to get the nth row), you could do something like:
UPDATE database_table
SET field = 'new_value'
WHERE primary_key IN (
SELECT primary_key
FROM database_table
ORDER BY some_date_column
LIMIT n - 1, 1
)
Edit: I should probably add a caveat. This answer might be technically correct, but you're likely wrong to use it. I can't imagine too many scenarios where you'd actually want to update the nth row of a table. You should generally only be updating tables based on primary keys. Updating the nth row will likely break your app if multiple users (or even multiple sessions with the same user) are using it at the same time.
The real answer is you should probably change your code to update based on primary key.
You would need some sort of id, and then do something like this:
UPDATE database_table SET field = 'new_value' WHERE id = 2

MySQL - how to insert values from one table into another with string matching?

MySQL noob here; looked around first but couldn't find the answer to this question.
So I have two tables in MySQL, one (Table1) which consists of one column containing unique list of names (Col1) and another containing corresponding binary values (Col2). The second table (Table2) contains a list of recurring names with a empty column waiting to be filled with binary values from the first table. What I want to do is, for each instance of a recurring name in Table2, insert the binary value from Col2 associated with the matching unique name in Table1.
I know how to do this in Excel—you just place the following VLOOKUP statement next to every row containing a recurring name. In the following code snippet A2 is a recurring name, unique names are contained in column B, and the binary values are contained in column C.
=VLOOKUP(A2,$B$2:$C$106095,2,FALSE)
But I can't for the life of me figure out how to reproduce this effect in MySQL. I can't use Excel because there's too much data. Anyone have any ideas? Thanks in advance!
I think that you want something like this (I don't know what the Excel statement does):
UPDATE table2 JOIN table1 ON table1.col1 = table2.col1
SET table2.col2 = table2.col2
WHERE table2.col2 IS NULL
This will update each row table2 that has col2 empty, searching for the corresponding row in table1 based on matching col1 columns.
Btw, do you have a reason to do this? Why not just join both tables when selecting the data? For example:
SELECT table2.col1, table1.col2
FROM table2 JOIN table1 ON table1.col1 = table2.col1

Multiple set and where clauses in Update query in mysql

I don't think this is possible as I couldn't find anything but I thought I would check on here in case I am not searching for the correct thing.
I have a settings table in my database which has two columns. The first column is the setting name and the second column is the value.
I need to update all of these at the same time. I wanted to see if there was a way to update these values at the same time one query like the following
UPDATE table SET col1='setting name' WHERE col2='1 value' AND SET col1='another name' WHERE col2='another value';
I know the above isn't a correct SQL format but this is the sort of thing that I would like to do so was wondering if there was another way that this can be done instead of having to perform separate SQL queries for each setting I want to update.
Thanks for your help.
You can use INSERT INTO .. ON DUPLICATE KEY UPDATE to update multiple rows with different values.
You do need a unique index (like a primary key) to make the "duplicate key"-part work
Example:
INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
ON DUPLICATE KEY UPDATE b = VALUES(b), c = VALUES(c);
-- VALUES(x) points back to the value you gave for field x
-- so for b it is 2 and 5, for c it is 3 and 6 for rows 1 and 4 respectively (if you assume that a is your unique key field)
If you have a specific case I can give you the exact query.
UPDATE table
SET col2 =
CASE col1
WHEN 'setting1'
THEN 'value'
ELSE col2
END
, SET col1 = ...
...
I decided to use multiple queries all in one go. so the code would go like
UPDATE table SET col2='value1' WHERE col1='setting1';
UPDATE table SET col2='value2' WHERE col1='setting1';
etc
etc
I've just done a test where I insert 1500 records into the database. Do it without starting a DB transaction and it took 35 seconds, blanked the database and did it again but starting a transaction first, then once the 1500th record inserted finish the transaction and the time it took was 1 second, so definetely seems like doing it in a db transaction is the way to go.
You need to run separate SQL queries and make use of Transactions if you want to run as atomic.
UPDATE table SET col1=if(col2='1 value','setting name','another name') WHERE col2='1 value' OR col2='another value'
#Frits Van Campen,
The insert into .. on duplicate works for me.
I am doing this for years when I want to update more than thousand records from an excel import.
Only problem with this trick is, when there is no record to update, instead of ignoring, this method inserts a record and on some instances it is a problem. Then I need to insert another field, then after import I have to delete all the records that has been inserted instead of update.