mysql cross-updating a table - mysql

I can't figure out how to solve this problem.
I have a table (A) I need to update, with a structure like this:
CODE VALUE
1 a
2 null
3 null
etc...
Then I have another table (B) with the same structure but with every value set:
CODE VALUE
1 a
2 b
3 c
What I need to do is to copy data from table B to table A where A.CODE = B.CODE but only if A.VALUE is not set.
What's the best query to do so? Can't do it by hand since I'm dealing with 2000ish rows.
I wrote something like this but it doesn't seem to work:
update A set VALUE =
(select b.VALUEfrom B b, A a where b.CODE = a.CODE)
Thanks in advance!

UPDATE a SET a.Value = b.Value
FROM TableA a
INNER JOIN TableB b ON (a.CODE = b.CODE)
WHERE a.VALUE IS NULL

Check that a.VALUE is set to do the update, and only select from the B table in the inner clause (and use the value of A from the outer clause):
update A
set VALUE = (select B.VALUE from B where B.CODE = A.CODE)
where VALUE IS NULL;

Related

How to complete this query function?

I have three tables.
Table A ###(Code, Value are combined primary key)
Code Value
1 | b
1 | c
3 | c
Table B
Value
b
Table C
Value
c
I would like to write a query of 'Code' from Table A.
The condition is that 'Code' should contain Value 'c'.
If 'Code' contains Value'b', this Code shouldn't be queried. (that's, Code 1 has value b and value c, so Code 1 needs to be excluded)
But I'm not available to do that query.
The expected outcome might be '3'
I want to use intersect but MySql doesn't contain this function yet.
So I tried some codes.
I'm sure that my codes have problems but I have no idea how to fix it.
SELECT DISTINCT A.*
FROM A B C
WHERE A.Value IN
(SELECT Value FROM B)
AND A.Value NOT IN
(SELECT Value FROM C);
Could you give me some tips on my questions?
the problem is that you are joining a, b and c. you almost got it.
select distinct a.value
from a
where a.value in (select value from b)
and a.value not in (select value from c)
or you can use join
select *
from a
inner join b where b.value = a.value
and not in (select value from c)

phpmyadmin mySQL Query for joins

I have a table A -
SNo ID Place
1 1000 Null
2 Null Null
3 1020 CityX
And another table B -
ID Place
1000 CityY
2000 CityZ
4040 CityAA
Now, I need to join table A and B such that I can get the values of Place in table A from table B. So my final table should look like this -
SNo ID Place
1 1000 CityY
2 1020 CityX
I'm trying to create an SQL query with joins, but that is only giving me empty rows. I did -
Select * from A
left outer join B
on A.ID = B.ID
where A.ID IS NOT NULL
Where is my query breaking? How do I get the expected result?
Select A.Sno, A.ID, IF(A.Place is null, B.Place, A.place) as Place from A
left join B
on A.ID = B.ID
where A.ID IS NOT NULL
Seems you need inner join
Select A.ID, ifnull(A.Place, B.Place) from A
Inner join B
on A.ID = B.ID
where A.ID IS NOT NULL
Technically, there is nothing wrong with your query, here is a working sqlfiddle of your question
http://sqlfiddle.com/#!9/29a002/1/0
Since you didn't post your actual schema or screenshot of it, I am going to suspect incompatible column definitions or invalid data
You can just do it like this Select the Place field of table B instead.
select SNo, B.ID, B.Place
from A
left outer join B
on A.ID = B.ID

MySQL Update only changes

I've imported a file to a MySQL table and now I want to update an existing table only with the changes.
I'm trying this with a select first before doing it as an update.
Both tables have an unique id field and value fields.
The following query doesn't give any results although there are differences in the value field:
SELECT a.id, a.value, b.value FROM a, b WHERE a.id=b.id AND a.value!=b.value
When I try it with a.value=b.value it works, but I need the changes.
Any Ideas?
My goal is to update table a in the end with changed values from table b but I can't update everything because I don't want to change a "lastedit" field if there are no changes.
I can't change the structure of table a to an automatic "lastedit" field.
UPDATE a INNER JOIN b ON a.id = b.id
SET a.value = b.value
WHERE a.value <> b.value
;
But of course, this only updates anything if (as Marc B said in his comment above) the tables have corresponding id values.

Get records not present in another table with a specific user id

I've this table (a)
And this table (b)
Now I have to get all records from A which are not present in B (a.id not present as b.idDomanda) and where B.idUser is not 1. So In this case, it should return only id 2 from a, but it returns 1 and 2.
This is my Query
SELECT a.* FROM a LEFT JOIN b ON a.id=b.idDomanda WHERE ( b.idUser <> 1 OR b.idUser IS NULL ) GROUP BY a.id
You want to move the condition on b to the on clause:
SELECT a.*
FROM a LEFT JOIN
b
ON a.id = b.idDomanda and b.idUser <> 1
WHERE b.idUser IS NULL
GROUP BY a.id;
The group by suggests that you might want to use not exists instead:
select a.*
from a
where not exists (select 1
from b
where a.id = b.idDomanda and b.idUser <> 1
);
There should be no results given your data set.
All records from A which are not present in B (a.id not present as b.idDomanda)
Given the test data set all of A is in fact IN b.idDomanda... even when filtering out userId = 1.
but as the previous person pointed out that is the query to check.

copy column values from one column to other

I am trying to copy data into one column (which is null for now) in table A from another similar column in table B. One condition I have is that I only want to copy values from the column in table B that is NOT NULL.
So far I have:
UPDATE a
SET a.[null column] = b.[original column]
FROM A as a
INNER JOIN B as b
ON a.id = b.idx
Any idea on how to do this?
.... where b.[original column] is not null
UPDATE a
INNER JOIN b AS b ON b.idx = a.id
SET a.column_name = b.column_name
WHERE a.column_name IN NULL
You can use a join like this. If there were schema it would be better
UPDATE a
SET a.[column] = b.[column]
FROM A as a
INNER JOIN B as b
on a.id = b.idx and b.[column] is not null