Update table using alias - mysql

I need to fill some fields in a table getting informations from other records of the same table.
I tried to write a query to explain what I want to do:
update globale2
set
nita = t.nita,
tita = t.tita,
notaita = t.notaita
where
neng = t.neng and
nita is null
(select nita, neng, tita, notaita from globale where uris='mma' and nita is not null) as t
edit to eplain better:
every records have these fields: "nita", "tita", "notaita", "neng" ("neng" cannot be null)
I want to fill these fields: "nita", "tita", "notaita" (where "nita" is empty)
with the same values from another record where "neng" equals the other "neng"

You can however, join the two tables.
UPDATE globale2 g
INNER JOIN globale gg
ON g.neng = gg.neng
SET g.nita = gg.nita,
g.tita = gg.tita,
g.notaita = gg.notaita
WHERE g.nita IS NULL
AND gg.uris = 'mma'
AND gg.nita IS NOT NULL

assume there is a table A_temp, with two columns 'one' and 'two'.
TABLE A_temp
ONE TWO
1 2
this is the present status of the table.
The query
UPDATE (SELECT * FROM A_temp ) A SET one = A.two where one = '1'
updates the table as
ONE TWO
2 2
Hope you get the idea and that it helps..

Related

How to Copy one table data to another table with 2 wherecondtion and an if condtion MYSQL

Actually i want to copy one tables data to another table. it has no unique id , the only relation between two are a "fra" number and a 'pra' number but both are not unique . but fra and pra (r concatenate ) is unique for each. and one table data is sex (customer table) and another is gender(new_customer table) the gender is Boolean, sex is string of m and f . how can i copy from customer table to new_customer table
I tried these way
UPDATE new_customer JOIN customer
SET registrations.name = customer.nam,
registrations.surname = customer.vornam,
registrations.ort = .ort,
registrations.phone = customer.telmbl,
registrations.surname = customer.vornam
WHERE registrations.fra = customer.fra
and registrations.pra = customer.pra;
Any body to help me?
something like following, you can try.
UPDATE new_customer AS new_c, customer AS old_c
SET
new_c.name = old_c.nam,
new_c.surname = old_c.vornam,
new_c.ort = old_c.ort,
new_c.phone = old_c.telmbl
WHERE
new_c.fra = old_c.fra
AND new_c.pra = old_c.pra;

finding a non existing value in one table that corresponds to a pairing in another table

I have two tables that i am working with. The first table is called temp6 with columns labeled as bsite, bsector, msite, msector, esite, esector, bpn, epn. The second table is called homdatapairs with columns labeled as fromcell, fromsector, tocell, tosector, hocount, percentage.
The goal is to produce a table that consists of bsite, bsector, msite, msector, hocount and percentage. The problem im running into is that the pairing of bsite, bsector, msite, msector does not exist in the homdatapairs table. So logically it should return the bsite, bsector, msite, msector, hocount and percentage leaving NULL values in hocount and percentage but it is not returning anything at all (no errors are occuring either).
Here is my query:
SELECT t6.bsite
, t6.bsector
, t6.msite
, t6.msector
, IFNULL(p.hocount, 0)
, IFNULL(p.percentage, 0)
FROM homdatapairs p
, temp6 t6
WHERE t6.bsite = p.fromcell
AND t6.bsector = p.fromsector
AND t6.msite = p.tocell
AND t6.msector = p.tosector;
I was able to figure it out. Here is the query that works.
SELECT DISTINCT t6.bsite, t6.bsector, t6.msite, t6.msector, IF(t6.msite = p.tocell AND t6.msector = p.tosector, IFNULL(p.hocount,0), 0), IF(t6.msite = p.tocell AND t6.msector = p.tosector, IFNULL(p.percentage, 0), 0)
FROM homdatapairs p, temp6 t6
WHERE t6.bsite = p.fromcell AND t6.bsector = p.fromsector;

Update table based on value inside this table

I'm trying to update a tablecolumn based on another value inside this table but I keep getting an error.
My query is as follows:
UPDATE partcreditor
SET partcreditor.creditorid = creditor.creditorid
FROM partcreditor
INNER JOIN creditor ON partcreditor.creditornr = creditor.creditornr
WHERE creditor.relgroupid = 1
AND creditor.creditortypeid = 1
UPDATE partcreditor AS PC
INNER JOIN creditor AS CR ON PC.creditornr = CR.creditornr
SET PC.creditorid = CR.creditorid
WHERE CR.relgroupid = 1 AND CR.creditortypeid = 1
No need to use the FROM clause in the update. As well as use alias name for better readability.

Can't update two tables in one Query

I've got two Queries to Update two tables:
First Table
UPDATE user_info SET `location` = ".$locationid.", `looking_for` = ".$lookingfor." WHERE `user_info`.`user_id` = ".$infoid.";
Second Table
UPDATE user_personality SET `personality` = '".$changedescription."' WHERE `user_personality`.`user_info_id` = ".$infoid.";
And I'm trying to merge those two Queries, using the same statement.
UPDATE user_info, user_personality
SET user_info.location = ".$locationid.", user_info.`looking_for` = ".$lookingfor.", user_personality.personality = '".$changedescription."'
WHERE `user_info`.`user_id` = ".$infoid."
AND `user_personality`.`user_info_id` = ".$infoid."
I'm not receiving any error message, but is not updating.
What am I doing wrong?
Thanks.
Just a guess...
"
UPDATE user_info i
JOIN user_personality p
ON p.user_info_id = i.user_id
SET i.location = $locationid
, i.looking_for = '$lookingfor'
, p.personality = '$changedescription'
WHERE i.user_id = $infoid;
";
If you set the 2 table fields equal to each other in the where clause it should work, so I believe you'd change your where clause to:
WHERE `user_info`.`user_id` = `user_personality`.`user_info_id`
AND `user_info`.`user_id` = ".$infoid."
MySQL definitely supports updating multiple tables, so the where clause that works for a multi table select statement should also work for an update.

How do I update a table with fields selected from another table?

Although there are many questions similar to this, such as
"Updating a record from another table", but i could not get this working.
I have a query that selects and updates table sem_stdexamfinresmark. The select subquery returns multiple rows of data whose size may not be equal to the table being updated, but the update is now working.
The query looks like :
update sem_stdexamfinresmark sr,
(select
se.currsession,
str.studentid,
str.classid,
str.subjectid,
str.aggScore*(select gbtp.percentage from gb_termpercentage gbtp where gbtp.termname = se.examtype)/100 as aggPer,
str.aggGrade
from
sem_stdexamtermresr str,
sem_exam se
where
str.examid=se.examid and
se.examtype = 'Second Term' and
se.currsession =1 and classid='8'
) s
set
sr.SecondTermMark = s.aggPer and
sr.SecondTermGrade = s.aggGrade
where
sr.studentid=s.studentid and
sr.subjectid=s.subjectid and
s.currsession = s.currsession and
sr.classid='8';
EDIT:
update sem_stdexamfinresmark
set
sr.SecondTermMark = s.aggPer and
sr.SecondTermGrade = s.aggGrade
from
(select
se.currsession,
str.studentid,
str.classid,
str.subjectid,
str.aggScore*(select gbtp.percentage from gb_termpercentage gbtp where gbtp.termname = se.examtype)/100 as aggPer,
str.aggGrade
from
sem_stdexamtermresr str,
sem_exam se
where
str.examid=se.examid and
se.examtype = 'Second Term' and
se.currsession = 1 and classid='8'
) s
where
sr.studentid=s.studentid and
sr.subjectid=s.subjectid and
s.currsession =1 and
sr.classid='8';
select * from sem_exam;
update sem_exam set currsession =1;
try something that looks more like:
update foo
set col = bar.col
from bar
where ...
This is what happens when one loses sleep :( I just did a silly mistake here and added "and"