Is it possible to achieve next thing without using views, but just one single query? I have two tables:
TableA->TanbleB (1-many) ON TableA.Id = TableB.TableAId
I need to update one field in Table A (TableA.Field1) for records in TableA that satisfy condition on one field in tableA (WHERE TableA.Field2=SomeValue)
.
TableA.Field1 will be updated from TableB with value that is last inserted (last inserted value in related records to TableA).
I will put an example:
UPDATE TableA a SET Field1 = (SELECT TOP 1 b.Feild1 * b.Field2 FROM TableB b WHERE b.TableAId = a.id) WHERE field2 = 1
I know Above example doesn't work, but I have many ways tried using INNER JOIN and failed. I had an idea to use something like this:
UPDATE TableA INNDER JOIN ( SELECT ... FROM TABLE B) ON TABLEA.Id= TableB.TableAId SET ....
But the 2ns query should return 1 record for each DISTINCT TableAId, but only the last inserted.
I hope I am making some sense here.
Thanks in advance.
Here is some SQL that will do what you want
UPDATE T1 INNER JOIN T2 ON T1.ID = T2.T1ID SET T1.F2 = [T2].[F2]*[T2].[F3] WHERE (((T1.F1)="ABC") AND ((T2.ID)=DMax("[ID]","[T2]","[T1ID]=" & [T1].[ID])));
This predicated on T1.ID being the primary key for T1 and T2.T1ID being a index field in T2
One of the flaws in Access is that you can't run an "UPDATE" query based on a "SELECT" query, it will usually give the error:
Operation must use an updateable query
The only way around is as you say to create a view of the "SELECT" query and then inner join this on your table, Access is then working with a static recordset and can handle the "UPDATE" query ok
Alternatively you could write a VBA procedure to step through line by line with the Recordset.
Best of luck : )
UPDATE:
SELECT b.TableAId, b.Feild1 * b.Field2 INTO tblView FROM TableB As b WHERE b.field2 = 1
Related
Is there a way to select a set of values from one table and then use those values in an IN clause?
I want to select IDs from one table and then update data for those IDs in another table.
So something like
<some var> = SELECT id from tableA WHERE <something>;
INSERT INTO tableB <stuff> where id IN (<some var>);
I release the variable syntax isn't real. just want to display my intent. I have read about SET a little but am still new to MySQL so it doesnt make perfect sense. Also it mentioned that SET could only set variables of certain types and they all seemed to simple.
Thanks!
You can use insert . . . select:
INSERT INTO tableB (id)
SELECT id
FROM tableA
WHERE <something>;
I'm not sure what IN has to do with this.
EDIT:
Oh, you want an update:
update tableb b join
tablea a
on b.id = a.id
set b.col = ??
where <conditions on a>;
You can also do this using in:
update tableb b
set col = ??
where b.id in (select a.id from tablea a where <conditions>);
The biggest difference is whether or not you want to use information from tablea. If you do, then you need the join version.
Got a small question in which I can't wrap my head around..
I have 2 tables, 1 table where there is a Youtube Link i need to append to another column in table 2. But not every record has a Youtube link, which means I need to check if in both tables the names are the same (WHERE name1 = name2?) But how can I append text to already existing text in table 2? Something like this?
UPDATE table2 SET text2 = (text2 + '/n' + table1.text1) WHERE name1 = name2?
If someone could help me, would be awesome!
EDIT:
So I fumbled a bit with the queries:
SELECT
'Kleding'.'Naam',
'jos_virtuemart_products_nl_nl'.'product_name',
CONCAT_WS('/n', 'jos_virtuemart_products_nl_nl'.'product_desc', 'Kleding'.'Youtube_link')
FROM
'jos_virtuemart_products_nl_nl' as 't2'
INNER JOIN 'Kleding' as 't1'
ON 't2'.'product_name' = 't1'.'Naam';
But this query is incorrect for some strange reason. I cannot find why it is incorrect.
You can use INNER JOIN in UPDATE:
UPDATE
table2 as t2 INNER JOIN table1 as t1 ON t2.name = t1.name
SET
t2.text2 = CONCAT_WS('/n', t2.text2, t1.text1);
P.S.: Maybe by '/n' you mean '\n' ?
P.P.S: Before any major data change operation I suggest you to backup your database.
UPDv1:
Test your end resultset with SELECT:
SELECT
`t1`.`name` as `t1-name`,
`t2`.`name` as `t2-name`,
CONCAT_WS('/n', `t2`.`text2`, `t1`.`text1`) as `changes`
-- ^ here you will see what comes up
FROM
`table2` as `t2` INNER JOIN `table1` as `t1` ON `t2`.`name` = `t1`.`name`
-- WHERE possibly more conditions to meet your requirements
If you manage te get proper resultset with additional WHERE conditions, then move them to the UPDATE statement.
Modified some stuff from my pic so you guys can understand it
I have this database. I am trying to update a value from a table based on another value from an another table.
I want to update the SUM from salary like this :
( sum = presence * 5 )
This is what I've been trying to use ( unsuccessful )
update table salary
set suma.salary = users.presence * 5
FROM salary INNER JOIN users1 INNER JOIN presence on id_salary = id_presence
I am not sure what to do, I'd appreciate some help, Thanks
In MySQL to UPDATE tables with a join you use this syntax:
UPDATE table1, table2
SET table1.column = some expression
WHERE table1.column = table2.column
That said, even with the updated picture, in your SQL you are mentioning columns that I cannot understand in which table are to be found. You also have an inner join between salariu and users1, with no join condition. Could you please clean up the question and make everything clear?
Assuming you are making the updates to the db structure you were talking about, then you can start working on this one maybe:
UPDATE salary, presence
SET salary.sum = SUM(presence.hours) * 5
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
Another way, but I'm not sure it is supported in all RDBMS, would be something like this:
UPDATE salary
SET sum = (
SELECT SUM(presence.hours) * 5
FROM user, presence
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
)
This SHOULD be trivial but I am going round in circles, perhaps someone can help.
I have two tables (T1, T2) from which I wish to extract a number of values in each row and update the contents of a third table (T3) iff (if and only if) two UQ, NN fields in T1, T2 match, in which case I want to add some of the values in the corresponding rows from T1, T2 together and put them in T3 and copy some other values over to T3.
The fields to be summed are all declared DECIMAL.
Simplified and in pseudocode (to avoid making too many assumptions):
SELECT T1.a,T1.b,T1.c from T1
SELECT T2.d, T2.e from T2
UPDATE T3.col1=a, T3.col2=b, T3.col3 = (value of(T2.c) + value of(T2.e)) iff T1.a = T2.d
A variety of attempts have failed to work.
I am running MySQL Workbench 5.2.37 on Ubuntu 12.10
Example from comment below:
UPDATE Test_join as T3
SELECT GZHident, Magnitude_1 from GZHTableExtended3 as T1
SELECT AHZid, DM from AHZDMConversionTable as T2 JOIN T2,T1
ON T1.GZHident = T2.AHZid
SET T3.AHZid = T1.GZHident
SET T3.DM = T2.DM
SET T3.Abs_Magnitude_1 = T1.Magnitude_1 + T2.DM;
MySQL supports multi-table UPDATE using JOIN syntax, just like SELECT. This isn't standard ANSI SQL, but it's very useful for these types of situations.
Example:
UPDATE T1
JOIN T2 ON T1.a = T2.d
JOIN T3 ON ...???...
SET T3.col1=T1.a,
T3.col2=T1.b,
T3.col3 = T2.c + T2.e;
You don't have enough information in your question for me to guess how to join to T3.
Thanks for posting an example UPDATE query. But it's still not clear how you want to update T3. The sample query you give is not valid SQL code at all. I suggest you read a tutorial on programming in SQL.
You seem be confused between UPDATE and INSERT? Keep in mind:
INSERT adds new rows.
UPDATE changes values in existing rows.
I'm guessing from your wording, but perhaps you want to add new rows. You can do that in this way:
INSERT INTO Test_join (AHZid, DM, Abs_Magnitude)
SELECT T1.GZHident, T2.DM, T1.Magnitude_1 + T2.DM
FROM GZHTableExtended3 as T1
JOIN AHZDMConversionTable as T2 JOIN T2
ON T1.GZHident = T2.AHZid;
I am trying to update the status of a column, by checking two joined tables. Even though I dont get an error. The colum is not updating. I want to take the general blockplot id and see if there is a transaction that matches and or a a container. If there is a transaction but no container i need to mark it as P.
UPDATE (general
LEFT JOIN
transactions
ON
general.blockplotid=transactions.blockplotid)
LEFT JOIN
container
ON
general.blockplotid=container.blockplotid
SET general.lotstatus = 'P'
WHERE general.lotstatus != 'U' AND
transactions.id_transaction IS NOT NULL AND
container.id_container IS NULL
So summarize, I have 3 tables. I only want to update one colum in one table. I want to check for values in the other two tables, their values depend upon the set value. The three tables are connected with a primary key to foreign key.
When I do a double join select statement. The query seems correct.
SELECT transactions.blockplotid AS blockplotid_2, container.blockplotid AS blockplotid_1, general.blockplotid, general.lotstatus, container.id_container, transactions.id_transaction
FROM ((general LEFT JOIN transactions ON general.blockplotid=transactions.blockplotid) LEFT JOIN container ON general.blockplotid=container.blockplotid)
ORDER BY general.blockplotid ASC
However it seems as though the join for the update isnt like the select.
This query seemed to work:
This query worked:
UPDATE ((general LEFT JOIN transactions ON transactions.blockplotid=general.blockplotid) LEFT JOIN container ON container.blockplotid=general.blockplotid)
SET general.lotstatus='P'
WHERE general.blockplotid!='U' AND container.id_container is null AND transactions.id_transaction is not null
The difference here is the case of IS NOT NULL and also the order of the where condition.
Is there any explanation for this?
I suspect the parentheses/locations of the joins. Try a setup like this:
UPDATE
Table1
SET
Table1
. Field1 = StagingTable . Field1
FROM
Table1
INNER JOIN StagingTable
ON Table1 . Field2 = StagingTable . Field2
WHERE
StagingTable . Field3 IS NOT NULL
I would suggest restructuring your UPDATE statement to have an embedded SUB-SELECT. update general set lotstatus = (SELECT .... WHERE .... )