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.
Related
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.
I Have a table in SQL with 2 columns:
ID, Date
Then I had to add another, column, "Description", that can be NULL. So my table will look like:
ID. Date. Description
01. 2000/08/07. NULL
02. 2000/03/01. NULL
03. 2001/08/17. NULL
..
99 2002/12/12. NULL
I have to update the column "Description" with a csv file.
I can't delete this table and each row should have a description
Is there a way to do that in workbench?
The final result should be:
ID. Date. Description
01. 2000/08/07. XY
02. 2000/03/01. XYZ
03. 2001/08/17. ZY
..
99 2002/12/12. ZX
Load the CSV file into a table, using load data infile. Presumably, the file has an id to link it to the existing data. Then use join:
update t join
csv_table c
on t.id = c.id
set t.description = c.description;
Import your csv as a table i.e. table-1. This table should have atleast one common column having distinct values as of the table you mentioned above. Then on the basis of joining, you may update your table for description attribute.
i'm not really an mysql guy more like a php guy :)
I have an issue with copying values from one table column to another table column.
The trick is that the data should be copied only if condition is matched. So basically i want to transfer categoryID from one table to another if postIDs are the same.
i have two tables news and news_in_category
in news table i have the following columns (id, title, categoryID)
in news_in_category i have the following columns (newsId, newsCategoryId)
So i want to move newsCategoryId to categoryID if newsId is the same as id.
For example if news table id=99 it should look up in news_in_category table find the newsId with value 99 and copy the newsCategoryId value to news table categoryID with id 99
Hope it makes sense to you :)
Thank you!
Try:
UPDATE news n
JOIN news_in_category nic ON n.id = nic.newsId
SET n.categoryID = nic.newsCategoryId
It looks like you might have a redundant functional dependency: newsId -> categoryId. If so, you could drop the news_in_category table without any loss of information, after running the query above.
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.
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.