Replace function in ACCESS - ms-access

I'm a beginner in MS ACCESS.
I need to join two tables: table_A and table_B. Since I want to use Replace function, but it doesn't work. My stupid code is:
UPDATE table_A
INNER JOIN table_B
SET table_A.name = table_B.name
ON table_A.age = Replace(table_B.age, "years-old","")
What's wrong with this?

You must fully define the data source(s) before the SET clause. So move the ON condition up one line:
UPDATE table_A
INNER JOIN table_B
ON table_A.age = Replace(table_B.age, "years-old","")
SET table_A.name = table_B.name
However that ON expression could be tricky. The Access query designer doesn't cooperate well with ON expressions which include functions. But the query could work if both table_A.age and table_B.age are text data type.
I suggest you first work this out as a SELECT query. Once you have the join set up and working, you can transform it from a SELECT to an UPDATE.

Related

H2 update with join

As development DB I am using MySQL, and for tests I am using H2 database.
The following script works in MySQL very well, but it is fails on H2.
UPDATE `table_a`
JOIN `table_b` ON `table_a`.id=`table_b`.a_id
SET `table_a`.b_id=`table_b`.id
In the internet I found that h2 doesn't support UPDATE clause with JOIN. Maybe there is a way to rewrite this script without JOIN clause?
By the way, I am using liquibase. Maybe I can write UPDATE clause with it's xml language?
I tried the following script
UPDATE table_a, table_b
SET table_a.b_id = table_b.id
WHERE table_a.id = table_b.a_id
But I still getting errors. Seems, that H2 doesn't support updating multiple tables in one query. How can I rewrite this query in two different queries to collect ids and insert them?
Try something like this:
update table_a a
set a.b_id = (select b.id from table_b b where b.a_id = a.id)
where exists
(select * from table_b b where b.a_id = a.id)
I've spend a lot of time for this kind of UPDATE. Please find out my comment, maybe somebody find it usefull:
For every rows in WHERE condition executed UPDATE for SET
In inner SELECT you can use updated table columns
In case of error "Scalar subquery contains more than one row" - UPDATE for SET return more, than one row. Problem rows could be found with replace UPDATE by SELECT COUNT(*)
See also Scalar subquery contains more than one row
Sample SELECT WITH UPDATE:
UPDATE USER_DETAILS UD SET UD.GRADUATE_COMMENT=
(SELECT U.COMMENT FROM USERS U WHERE u.ID=UD.id) <-- ref to outer updated table
WHERE UD.GRADUATE_COMMENT IS NULL;

GET * Datas from first table connect over same ID

i guess that i ask a kind of question like it was ask a thousand times, before, but i dont understand the part in other questions, i hope someone could explain it me at my simple code.
I have two tables
TableA -> ID|SITEID|NEXT|...
TABLEB -> ID|SITEID|ANOTHER|...
Now i want to catch all results wich are matched by the same SITEID='SITEXY' and TABLEB.ANOTHER='IDXY'. As result i only want to recieve the fields of TABLEA.
At the moment i do it this way, but i get the fields from both tables.
SELECT * FROM TABLEA, TABLEB WHERE TABLEA.SITEID='SITEXY' AND TABLEB.ANOTHER='IDXY' AND TABLEA.SITEID=TABLEB.SITEID;
Mybe its better to use "USING" or "JOIN" but i'm to stupid to understand how it works....:-(
You can qualify the wildcard with the table from which you want to get the rows:
select TABLEA.*
from TABLEA
join TABLEB on TABLEA.SITEID = TABLEB.SITEID
where TABLEA.SITEID = 'SITEXY'
and TABLEB.ANOTHER = 'IDXY';
Also, always use modern explicit join syntax instead of comma based join.
Using aliases, you can make the query bit cleaner:
select a.*
from TABLEA a
join TABLEB b on a.SITEID = b.SITEID
where a.SITEID = 'SITEXY'
and b.ANOTHER = 'IDXY';
Assign aliases to both tables, and then select all columns from TABLEA:
SELECT a.*
FROM TABLEA a
INNER JOIN TABLEB b
ON a.SITEID = b.SITEID
WHERE a.SITEID = 'SITEXY' AND
b.ANOTHER = 'IDXY';
Aliases make it easier to read and write a query. Note that I have also replaced your implicit join with an explicit one using INNER JOIN and ON. As a general rule, you should avoid writing commas in the FROM clause.

How do I list records in a table only if a field is not present in another table?

First time posting here so please excuse my manners or lack thereof.
My issue is related to a single MySQL database.
How do I use a query to list all records where a specific field is not found in another table's specific field? So far I can use left outer join to show matches but I want to show misses.
select * from TABLE_A
left outer join TABLE_B
on (TABLE_B.id = TABLE_A.field)
where TABLE_B.id is not null;
How do I syntax a query to show records that do not have a match in TABLE_B.field? Leveraging "is null" provides no results.
A left join with where Table_B.id is null should work.
If you are not getting any results, it could be that there is no where the table_B = Table_A.field do not match.
I would suggest looking at your expect data output again more carefully.

How to UPDATE in one table in SQL based on matching data in another table?

I found several questions with similar wording, but none addressed the specific question I have.
How does one perform an UPDATE with conditions that operate between two unlinked tables?
As example
TABLE_I
ID, Placed, junk, junk, junk
TABLE_II
ID, Category, Placed, Note, junk, junk...
If the Condition is in TABLE_II
WHERE Category=9 AND Note=#testvalue
An UPDATE should take place where a value in TABLE_II matches one in TABLE_I
UPDATE TABLE_I SET Placed=#testvalue WHERE
.. the Current TABLE_I.Placed=Table_II.Placed assuming the above conditions are met
Is such stepped-in conditioning even possible in SQL? Or would it require coding outside of the query to test in steps?
SQL
update t1 SET t1.Placed=#testvalue
from Table_1 t1
join Table_2 t2 on t1.placed = t2.placed
where t2.Category=9 AND t2.Note=#testvalue
you have to use join in the update statement
Mysql
the answer is yes you can
try it like that
update Table_1 t1
join Table_2 t2 on t1.placed = t2.placed
where t2.Category=9 AND t2.Note=#testvalue
SET t1.Placed=#testvalue
EDIT:
For general Update join :
UPDATE TABLEA a
JOIN TABLEB b ON a.join_colA = b.join_colB
SET a.columnToUpdate = [something]
You could do this kind of stuff with a trigger on insert on table_i combined with a procedure for updating the first table.
Not sure on how to do it in MySQL or SQL-server (why 2 tags? Which is it?), but it probably is not much different from doing this in PostgreSQL. A quick Google search gave me http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html
Though the best solution probably is to actually link the unlinked tables.

Optimal way to fill in missing values after a LEFT JOIN?

I will simplify my problem to make the core issue clear: I have a query that does TableA INNER JOIN TableB LEFT JOIN TableC.
The result of the left join is that in the result set, two of the columns might have NULL values in some rows. To fill in the missing values I have to loop over the result set and query another database that has the data (so it is not possible to join
in the first place).
My question is: Is there a standard/optimised approach when we need to fill nulls of a result set after a left join?
You can use COALESCE(...) (MSDN - COALESCE) instead.
You query will then look like:
select a, b, COALESCE(TableB.c, 'replacement value')
from TableA INNER JOIN TableB LEFT JOIN TableC ...
Add another join for your replacement table and put the column you want to replace NULL values in the COALESCE function in you don't want to use a static value.
"To fill in the missing values I have to loop over the result set and query another database that has the data (so it is not possible to join in the first place)."
Consider a different solution then looping to fill in data.
1.Another database on the same server==easy. Just join using db.schema qualified names.
Another database on a another server, still possibly depending on you network topography. Then join using server.db.schema qualified names.
Consider replicating the data you need if you regularly need to do this.