I'm trying to copy the contents of a column in one mysql database to an identical table in another mysql database.
I'm using:
UPDATE db1.table
SET db1.table.name = db2.table.name,
db1.table.address = db2.table.address
WHERE db1.table.id = db2.table.id;
I'm getting the error 1054: Unknown column 'db2.table.id' in 'where clause'.
Both tables have an id column, so I'm not sure why it won't work. I'm logged in as admin, and have full rights to both databases.
UPDATE db1.table
JOIN db2.table
ON db1.table.id = db2.table.id
SET db1.table.name = db2.table.name,
db1.table.address = db2.table.address
Related
I am working on a mysql procedure that I can run on all of my client environments however I am running into one issue. Some of my clients have a particular column on the database and others do not.
I am trying to use update ignore but I still get an error if the column doesnt exist.
I know I can query information schema for a count where that column exists, but I'm wondering if there is a more simplistic way to achieve this.
update ignore table1 set columnA = null;
ERROR 1054 (42S22): Unknown column 'columnA' in 'field list'
You have two options:
First solution: Check first if the column exists.
SELECT t.table_name, (c.column_name IS NULL) AS columnA_present
FROM INFORMATION_SCHEMA.TABLES AS t
LEFT OUTER JOIN INFORMATION_SCHEMA.COLUMNS AS c
ON t.table_name = c.table_name and c.column_name = 'columnA'
WHERE t.table_name = 'table1';
Second solution: Run the UPDATE and catch the error. Check if the error is 1054. If so, then skip it. If not, then it's some other error so report it.
I am trying to select all rows from all tables except for 2 tables in SQL for use in PHP calculations. I am trying run the follow query:
SELECT MovieID, Rating FROM information_schema.tables WHERE table_schema = 'moviedb' AND TABLE_NAME <> ('movies', 'users');
This is returning "ERROR 1054 (42S22): Unknown column 'MovieID' in 'field list'" in SQL.
All tables except 'movies' and 'users' have identical columns but varying data and amounts of data (some are all null).
I have tried using EXCEPT and it doesn't seem to work on the version of SQL we are running. It's for a school project on a school server so I have to use the version available.
I am trying to update a column in a table of one database to other table in a different database. Here is my query:
update mr set reqprofile_id=subhamastu.response_to from matrimonyrequests mr INNER JOIN subhamastu.matrimony_response smr ON mr.reqid=smr.matrimony_response_id;
I am getting "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from matrimonyrequests mr INNER JOIN subhamastu.matrimony_response smr ON mr.req' at line 1"
I have tried the above query. Actually i want to update a column which matches a table column email id to a table called register and get profileid and update in
my table. I am attaching a structure of two databases.
I want to update the column in requesttable from 'old' database responsetable column response_to, however, i want to put the profileid that matches the email in response table in new database requesttable. Quite difficult to explain. I don't know i conveyed my message correctly or not. So attached the structure of the image. Hope somebody helps...
The correct MySQL syntax is:
update matrimonyrequests mr INNER JOIN
subhamastu.matrimony_response smr
ON mr.reqid=smr.matrimony_response_id
set reqprofile_id = subhamastu.response_to ;
Your syntax looked more like SQL Server.
The following mysql query
select * from PrivateData.Table1 AS m
LEFT JOIN
PrivateData.Table2 AS i
ON m.GUID = i.OutId;
works just fine while this query:
delete m,i from PrivateData.Table1 AS m
LEFT JOIN
PrivateData.Table2 AS i
ON m.GUID = i.OutId;
gives an error:
ERROR 1046 (3D000): No database selected
Why is that the case? How can i delete the selected rows from the tables otherwise?
P.S.: I am no expert in mysql queries.
I am not sure whether it is well-related to the question but worth a try. From documentation:
For alias references in the list of tables from which to delete rows
in a multiple-table delete, the default database is used unless one is
specified explicitly. For example, if the default database is db1, the
following statement does not work because the unqualified alias
reference a2 is interpreted as having a database of db1:
DELETE a1, a2 FROM db1.t1 AS a1 INNER JOIN db2.t2 AS a2
WHERE a1.id=a2.id;
To correctly match an alias that refers to a table outside the
default database, you must explicitly qualify the reference with the
name of the proper database:
DELETE a1, db2.a2 FROM db1.t1 AS a1
INNER JOIN db2.t2 AS a2
WHERE a1.id=a2.id;
Try to specify explicitly your database:
delete PrivateData.m, PrivateData.i from PrivateData.Table1 AS m
LEFT JOIN
PrivateData.Table2 AS i
ON m.GUID = i.OutId;
Little modifications will work
delete from m,i using PrivateData.Table1 AS m LEFT JOIN PrivateData.Table2 AS i ON m.GUID = i.OutId;
MySQL ERROR 1046 (3D000): No database selected:
simple one:
You need to tell MySQL which database to use:
USE database_name;
before you create a table.
In case the database does not exist, you need to create it as:
CREATE DATABASE database_name;
followed by:
USE database_name;
Another one:with brief example
While I was trying to view the tables in a given database, I encountered the above error. What happens is that when you connect to a MySQL server, it doesn't choose a database for you unless you explicitly specify it.
If you only use,
mysql --userusername --passwordpassword
or
mysql -uusername -ppassword
then the MySQL client just connects to the database server. After this, you will have to specify the database name, which you intend to use:
use database_name
If you don’t like the above method, what you can do is directly specify the database at startup only.
The syntax for the same is:
mysql -uusername -ppassword database_name
For ex., mysql –ugaurav –pchhabra testdb
click to sqlfiddle
I am trying to use the new "MERGE" statement in SQL Server 2008. The statement will get records from a temporarty table and update the same in some other table.The statement is as following:
create table #TempTable(ProcPOAmdDel_ProcessAmendmentId bigint,ProcPOAmdDel_SemiFinProdId bigint,ProcPOAmdDel_ChallanQty int)
MERGE PurProcessPOAmendmentDelivery AS pod
USING (SELECT ProcPOAmdDel_ProcessAmendmentId,
ProcPOAmdDel_SemiFinProdId FROM #TempTable ) AS temp
ON pod.ProcPOAmdDel_ProcessAmendmentId = temp.ProcPOAmdDel_ProcessAmendmentId AND
pod.ProcPOAmdDel_SemiFinProdId=temp.ProcPOAmdDel_SemiFinProdId
WHEN MATCHED THEN UPDATE
SET pod.ProcPOAmdDel_ChallanQty = temp.ProcPOAmdDel_ChallanQty;
While running the state I encountered an error Invalid column name'ProcPOAmdDel_ChallanQty'.
Could anybody help me in resolving the issue?
Include column ProcPOAmdDel_ChallanQty in Source table i.e. temp
MERGE PurProcessPOAmendmentDelivery AS pod
USING (SELECT ProcPOAmdDel_ProcessAmendmentId,
rocPOAmdDel_SemiFinProdId,
ProcPOAmdDel_ChallanQty
FROM #TempTable ) AS temp
ON pod.ProcPOAmdDel_ProcessAmendmentId = temp.ProcPOAmdDel_ProcessAmendmentId AND
pod.ProcPOAmdDel_SemiFinProdId=temp.ProcPOAmdDel_SemiFinProdId
WHEN MATCHED THEN
UPDATE SET pod.ProcPOAmdDel_ChallanQty = temp.ProcPOAmdDel_ChallanQty;