I have this table:
datatable
I would like to write a single query to update whole 99 by below number so for each 99 will be replaced by number below, for each blank name, the name below.
I tried:
update datatable.code set datatable.code= previous (datatable.code)
WHERE datatable.code=99;
update abc set code =
CASE WHEN name='Jean' THEN 1
WHEN name='Peter' THEN 2
END
where code=99;
use mysql without update
select *,
if(ta.code!=99,ta.code, select ta2.code from table_a as ta2 where ta2.ID_q=ta.ID_q and ta.code!=99 limit 1) as code_a,
if(ta.name!='',ta.name, select ta2.name from table_a as ta2 where ta2.ID_q=ta.ID_q and ta.name!='' limit 1) as name_a
from table_a as ta;
Related
Once I have the two tables compared and they give me what has changed, I would like the first table to be updated with the new data from the second one.
This is my code where I put in my phpmyadmin database that compares the two tables:
SELECT
codice_Fiscale,nome,cognome,etichetta,sesso,residenza,
cellulare,email,telefono,id_vitaever
FROM (
SELECT codice_Fiscale,nome,cognome,etichetta,sesso,residenza,
cellulare,email,telefono,id_vitaever
FROM operatore
UNION ALL
SELECT codice_Fiscale,nome,cognome,etichetta,sesso,residenza,
cellulare,email,telefono,id_vitaever
FROM operatoreImport
) tbl
GROUP BY codice_Fiscale,nome,cognome,etichetta,sesso,residenza,
cellulare,email,telefono,id_vitaever
HAVING count(*) = 1
ORDER BY codice_Fiscale
I have this tables:
TABLE 1:
id name
1 Oriol
2 Ricard
TABLE 2:
id name
1 Uriol
2 Ricard
And once compared the two tables, I want this:
TABLE 1:
id name
1 Uriol
2 Ricard
maybe this code help you
UPDATE table1 t1, table2 t2
SET t1.name=t2.name, t1.surname=t2.surname
WHERE t1.id=t2.id
I have a table with 2 columns
NAME SYNONYM
----------------
A ALPHA
B ALPHA
B BITA
C GAMA
D DELTA
E BITA
I am looking for a SQL query that will check column SYNONYM. If it finds the same SYNONYM, for example it finds ALPHA in second row which is the same with row 1 it will change B and make it A. The change of B will take place everywhere in column NAME, not only in one row.
NAME SYNONYM
----------------
A ALPHA
A ALPHA
A BITA
C GAMA
D DELTA
A BITA
If it is difficult to alter the column NAME we could add a new column like this
NAME SYNONYM NEW
----------------------------
A ALPHA 1
B ALPHA 1
B BITA 1
C GAMA 2
D DELTA 3
E BITA 1
NOTE: This type of query is not possible in straight MySQL if its supposed to be a dynamic query... MySQL does not support recursive queries.. however if you know the column you would like to update then you can do this query in a roundabout way.
So if there are some columns you would like to update you can do it like this and it'll do exactly what you're asking for..
EDIT:
if you use user defined variables you could walk through the table like so:
SET #A := (SELECT DISTINCT name FROM example_table ORDER BY name LIMIT 0,1);
UPDATE example_table et,
(
SELECT
DISTINCT synonym
FROM example_table
WHERE name IN
(
SELECT
DISTINCT name
FROM example_table
WHERE synonym IN
(
SELECT
DISTINCT synonym
FROM example_table
WHERE name = #A
)
)
) t
SET et.name = #A WHERE et.synonym = t.synonym;
all you have to do is increment the 0 in the SET #A statement.. LIMIT 0,1... LIMIT 1,1.... that will give you a new distinct name each time that you can execute the update on. Hope that helps
I need to select all the values of table 1 from the first database that are not present in table 2 from the second database. I tried the code below, but DISTINCT does not work:
select DISTINCT(affected_ci),ci_name from sitequota.incidents,appwarehouse.ci_table where incidents.affected_ci <> ci_table.ci_name
DATABASE1: APPWAREHOUSE
TABLE1: CI_TABLE
COLUMN: CI_NAME
DATABASE2: SITEQUOTA
TABLE2: INCIDENTS
COLUMN: AFFECTED_CI
You could try something like:
SELECT ci_name
FROM appwarehouse.ci_table
WHERE ci_name NOT IN
(SELECT affected_ci FROM sitequota.incidents
)
I have a table with a field have values 1,2,3,5,6,....
My problem is,When i try to insert "2" current 2,3 are incremented with 1 and 5,6 remains unchanged(because 4 is not exist),Then in the place of 2 new value needs to be inserted.
I try update query with
Update table set v1=v1+1 where v1>=newvalue
before insert a new value.But 5,6 also incremented with 1.I dont know how to do this.Does anyone know how to handle this ,Please help me.I have large amount of data,so not possible to make loop for update each value
SELECT t1.v1 + 1 AS gap
FROM `table` AS t1
LEFT OUTER JOIN `table` AS t2 ON t2.v1 = t1.v1 + 1
WHERE t2.v1 IS NULL AND t1.v1 >= :newvalue
ORDER BY t1.v1 ASC
LIMIT 1
This query gives the upper bound for updating v1 when inserting :newvalue.
I have 2 tables: tbl_taxclasses, tbl_taxclasses_regions
This is a one to many relationship, where the main record ID is classid.
I have a column inside the first table called regionscount
So, I create a Tax Class, in table 1. Then I add regions/states in table 2, assigning the classid to each region.
I perform a SELECT statement to count the regions with that same classid, and then I perform an UPDATE statement on tbl_taxclasses with that number. I update the regionscount column.
This means I'm writing 2 queries. Which is fine, but I was wondering if there was a way to do a SELECT statement inside the UPDATE statement, like this:
UPDATE `tbl_taxclasses` SET `regionscount` = [SELECT COUNT(regionsid) FROM `tbl_taxclasses_regions` WHERE classid = 1] WHERE classid = 1
I'm reaching here, since I'm not sure how robust MySQL is, but I do have the latest version, as of today. (5.5.15)
You could use a non-correlated subquery to do the work for you:
UPDATE
tbl_taxclasses c
INNER JOIN (
SELECT
COUNT(regionsid) AS n
FROM
tbl_taxclasses_regions
GROUP BY
classid
) r USING(classid)
SET
c.regionscount = r.n
WHERE
c.classid = 1
Turns out I was actually guessing right.
This works:
UPDATE `tbl_taxclasses`
SET `regionscount` = (
SELECT COUNT(regionsid) AS `num`
FROM `tbl_taxclasses_regions`
WHERE classid = 1)
WHERE classid = 1 LIMIT 1
I just needed to replace my brackets [] with parenthesis ().