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 .... )
Related
I have the below code:
UPDATE VIEW
LEFT JOIN sem_view ON (view.semester = sem_view.semester)
SET view.t_credit = SUM(sem_view.credit)
but its not working saying invalid use of group
Data for updating must be perpared previously, in subquery. Updating must use the data which is already aggregated.
Left joining may be errorneous - it will set the value in the destination table to NULL if according data for the semester in interest in sem_view is not present. But if the logic needs this then you may use LEFT JOIN instead of INNER one in the below query.
Totally:
UPDATE `view`
JOIN ( SELECT sem_view.semester, SUM(sem_view.credit) summ
FROM sem_view
GROUP BY sem_view.semester ) data_for_updating
ON `view`.semester = data_for_updating.semester
SET `view`.t_credit = data_for_updating.summ;
PS. The text looks like view is a name of a table to be updated.
I am not sure how to ask this question - it might be answered somewhere, but it's kind of messy ;)
There are two tables that sort flags (labels) for objects somewhere else.
Table 1: the flags in booka_flags
columns of interest are id(INT) and title(VARCHAR)
Table 2: the flag items in booka_flags_items
columns of interest are id(INT) and flag(VARCHAR)
I want to change booka_flags_items.flag from VARCHAR (which is available from booka_flags.title) to ID (which is available from booka_flags.id)
My "idea" of writing this query (after some research) was the following:
UPDATE
booka_flags_items
SET
booka_flags_items.flag = booka_flags.id
FROM
booka_flags_items
INNER JOIN
authors
ON
booka_flags_items.flag = booka_flags.title
but it does not work. Also not working is this query:
UPDATE
booka_flags_items
SET
booka_flags_items.flag = (
SELECT booka_flags.id
FROM booka_flags
INNER JOIN booka_flags_items
ON booka_flags_items.flag = booka_flags.title
)
What would be the right way to solve a query like that?
I'm guessing you would like to update id column of booka_flags with id column of booka_flags where title matches flags. Your updated query looks incorrect as per mysql
Try:
UPDATE booka_flags_items t1 INNER JOIN booka_flags t2 On t1.flag = t2.title
SET t1.id = t2.id
I'm looking for a simple way to do an update on a table only if there is no other columns present in that same table with the same value I'm trying to update, ideally in a single query. So far I'm getting an error You specify target table 't1' for update in FROM clause. Here is what I tried in a few variations so far (still unable to get working):
UPDATE emailQueue AS t1
SET
t1.lockedOn = 1470053240
WHERE
(SELECT
COUNT(*)
FROM
emailQueue AS t2
WHERE
t2.lockedOn = 1470053240) = 0
AND t1.lockedOn IS NULL
In MySQL, you need to use a join. In this case, a left join is in order:
UPDATE emailQueue eq LEFT JOIN
emailQueue eq2
ON eq2.lockedOn = 1470053240
SET eq.lockedOn = 1470053240
WHERE eq.lockedOn IS NULL AND
eq2.lockedOn IS NULL;
In mysql I have tableA with rows userid and valueA and tableB with userid and valueB.
Now I want all entries from tableA which don't have an entry in tableB with the same userid.
I tried several things but can't figure out what I do wrong.
SELECT * FROM `tableA`
left join `tableB` on `tableA`.`userid` = `tableB`.`userid`
This is a very good start actually. It gives me all entries from tableA + the corresponding values from tableB. If they don't exist they are displayed as NULL (in phpmyadmin).
SELECT * FROM `tableA`
left join `tableB` on `tableA`.`userid` = `tableB`.`userid`
where `tableB`.`valueB` = NULL
Too bad, empty result. Maybe this would have been too easy. (By the way: tableA has ~10k entries and tableB has ~7k entries with userid being unique in each. No way the result would be empty if it would do what I want it to do)
SELECT * FROM `tableA`
left join `tableB` on `tableA`.`userid` = `tableB`.`userid`
where `tableA`.`userid` != `tableB`.`userid`
This doesn't work either, and to be honest it also looks totally paradox. Anyways, I'm clueless now. Why didn't my 2nd query work and what is a correct solution?
You are almost there. That second query is SO close! All it needs is one little tweak:
Instead of "= NULL" you need an "IS NULL" in the predicate.
SELECT * FROM `tableA`
left join `tableB` on `tableA`.`userid` = `tableB`.`userid`
where `tableB`.`valueB` IS NULL
^^
Note that the equality comparison operator = will return NULL (rather than TRUE or FALSE) when one side (or both sides) of the comparison are NULL. (In terms of relational databases and SQL, boolean logic has three values, rather than two: TRUE, FALSE and NULL.)
BTW... the pattern in your query, the outer join with the test for the NULL on the outer joined table) is commonly referred to as an "anti-join" pattern. The usual pattern is to test the same column (or columns) that were referred to in the JOIN condition, or a column that has a NOT NULL constraint, to avoid ambiguous results. (for example, what if 'ValueB' can have a NULL value, and we did match a row. Nothing wrong with that at all, it just depends on whether you want that row returned or not.)
If you are looking for rows in tableA that do NOT have a matching row in tableB, we'd generally do this:
SELECT * FROM `tableA`
left join `tableB` on `tableA`.`userid` = `tableB`.`userid`
where `tableB`.`userid` IS NULL
^^^^^^ ^^
Note that the IS NULL test is on the userid column, which is guaranteed to be "not null" if a matching rows was found. (If the column had been NULL, the row would not have satisfied the equality test in the JOIN predicate.
Change = NULL for IS NULL on your code. You can also use NOT EXISTS instead:
SELECT *
FROM `tableA` A
WHERE NOT EXISTS (SELECT 1 FROM `tableB`
WHERE `userid` = A.`userid`)
this will help explain the use of working with NULL values:
http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html
the less expensive query would be this, all the other queries sugested are too expensive if the database is too big:
SELECT * FROM tableA a
WHERE a.userid NOT IN
(SELECT b.user_id FROM tableB b);
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