How to update all fields form one table to another [MySQL] - mysql

I have two tables with a lot of fields
table - A
table - B
in table A only two fields are filled all other are empty, but exactly that fields are filled in table B
I would like to
UPDATE A, B set A.c = B.c, A.d = B.d .... WHERE ....
but there are a bout 100 columns, is there any way how to update all fields in A from B except 1 particular field ? Is there any way how to tell mysql left 1 particular fields in A as it is.

If field name in table A and B are same then you can just use excel to build the string.
Copy the entire structure of DB table A into excel as 1 column and then use excel formula with string concatenation to build the required string.
It should not be that difficult task.
From MySql end, you can review DELETE and INSERT option with help of temporary table. More details needed to think of solution at MySql end.

Related

Update table in one schema based on another schema table column in same database

I have two schemas in the same database. I want to update a table in one schema based on the table in another schema where the column value matches.
The structure of my database is as follows.
structure of database
As an example, this is my table named "p" in 1st schema that is "public" and have column "id" and "name"
id name
3 Loral
1 Kim
2 Shawn
and this is the second table named "t" in the second schema that is "t_sc" and have column "id" and "name" but the names are different than table "p"
id name
1 kylie
3 deny
2 tom
Now I want to update table "p" names according to table "t" names where the id matches.
I have tried the following query
update p set p.Name = t_sc.t.Name WHERE p.ID = t_sc.t.ID
but got the following error
ERROR: missing FROM-clause entry for table "t"
I have tried multiple other ways too but I am not able to get the desired result. I am using Navicat for query and the database is Postgresql.
If this is really MySQL, you need to call your schema(s) specifically.
You can thank this user for the example below:
Update a column on a table from a table on another schema MySql
update schema1.table1
inner join schema2.table2
on schema1.table1.IDColumn = schema2.table2.IDColumn
set schema1.table1.column1 = schema2.table2.column2
I have found the answer myself. So I thought maybe I should post it for future visitors.
The query will be
UPDATE public.p a
SET name=b.name
FROM t_sc.t b
WHERE a.id=b.id
I was using the alias with column name p.Name of the Table which I want to update. Also I was using the schema name at the wrong time t_sc.t.Name It was causing the error.

how to separate 1 table into 2 tables and add a new column?

i have 1 table with i want to separate it into 2 tables and i want to add a new column to the new table. how can i do that?
example :
I have table A with column : a, b, c
and i want to copy the data into new table ( Table B and C )
table B with column : a, e,
table C with column : b, c, f
example table
I've tried
CREATE TABLE C as SELECT A.b, A.c FROM A;
but i dont know how to add the new column (c and f). and if i already have hundreds of data what is the best way to input data into these new columns? should i do it one by one? im new to programing and sql, thank you for helping me.
to add new column, you need to use ALTER.
ALTER TABLE table_name
ADD column_name datatype;
Source: W3Schools also it will be a good idea to read the entire thing for mysql. you will learn a lot
but as a warning. make sure you understand what you are doing, the column can be removed later on. but making mistakes can damage your data.
if you need to alter your tables for a specific task only then I suggest creating temporary tables that you can then do whatever you need with. and then discarding them

Is there any way to replace whole column data in mysql?

I have a table with 62 columns and 22k rows. I need to change every row of the first column. I have the new data on a text file. How can I do it?
I have tried with 'replace' and 'update' but I can't find the way to substitute using the text file and it's impossible to change it manually.
Let's say your original table looks like this (it would be helpful if you'd post snippets or samples):
Name ID
==== ==
Mary 1
John 2
Dave 3
Suzy 4
And your text file:
Mari
Juan
Daav
Suzi
If you know that the sequence of lines in the text file is the same as the table (or you can arrange the table in that sequence by query), then do the following:
Create a temporary table by importing the text file. If necessary add a unique ID that you can use as a join criteria.
Write an update query that links the tables as needed:
UPDATE table a
LEFT JOIN temp_table b
ON a.id = b.id
SET a.name = b.name;
Research how to add a primary or unique key to either table as needed to create the join condition.

How can I copy one column (field) from one table to an empty column (field) of another table both of which are part of same database using MySQL?

I'm using MySQL with Sequel Pro on a Mac OS X. I would like to copy one field (i.e., a column called "GAME_DY") from one table into an empty field of another table called "DAY_ID". Both tables are part of the same database. I've searched the answers to similar/same questions, but cannot find an answer with manipulable code that works.
Database: retrosheet
Table 1 (Existing table called "game") from which I would like to copy field "GAME_DY"
Fields:
GAME_ID,
YEAR_ID,
GAME_DT,
GAME_CT,
GAME_DY,
START_GAME_TM,
....
Table 2 (existing Table called "starting_pitcher_game_log") to which I would like to copy to field "DAY_ID":
Fields:
PIT_ID,
GAME_ID,
W,
L,
IP,
BFP,
H,
R,
DAY_ID,
DATE
....
I want to copy "GAME_DY from TABLE 1 into "DAY_ID" in TABLE 2.
Can this be done using MySQL queries?
If you want to update existing records in the starting_pitcher_game_log field, you could use SQL like this:
UPDATE starting_pitcher_game_log SET DAY_ID = (
SELECT GAME_DY FROM game, starting_pitcher_game_log
WHERE game.GAME_ID = starting_pitcher_game_log.GAME_ID
)

Automatic Update for database rows based on changes in another table

I have a scenario that requires a value in a row of a table to be updated automatically whenever a row has been added or deleted in another table. I'm not sure how to do it.BTW I'm using phpmyadmin in order to manage my database. Thanks in advance.
pages Table
------------
page_no
no_of_choices
choices Table
-------------
page_no
choice_no
When I add a choice with choice number 1 and page_no, then the table page which has the row, page_no=1 should be updated with no_of_choices=no_of_choices+1
You can use triggers.
For example:
CREATE TRIGGER `test1`
AFTER INSERT ON `tbl1`
FOR EACH ROW SET NEW.upd_fld = new_value
Similarly could be done for delete.
You can also create triggers from phpMyAdmin
TABLE A: page_no, no_of_choices
TABLE B: page_no, choice_no...
With a relational database you very rarely want to have duplicate data. If something breaks at some point, you won't know which to trust - the rows in Table B, or the no_of_choices in Table A. A better solution is to do one of the following (depending on which table you are querying):
SELECT COUNT(no_of_choices) FROM B WHERE page_no = 1
or
SELECT A.*, COUNT(choice_no) AS choice_no FROM A LEFT JOIN B USING(page_no)
You get the same result, but now you have one record to go off of, so you won't have inconsistent data.