insert data from one table to another - Not Working - possible bug? - mysql

I'm trying to insert data from one table to another, but I keep getting a weird SQL error. The following is my query along with the error.
insert into hs.hs (`field1`,`field2`,`field3`) select cid,sid,'1' from `tmp1`;
now the error
1416 - Cannot get geometry object from data you send to the GEOMETRY field.
I don't understand why I'm getting a geometry error there is no geometry involved at all.
By the way my MySQL version is:
SELECT VERSION(); Result 5.7.16-log
I'm pretty sure I'm doing everything right; Can some one please tell me what I'm doing wrong?
Could this be a bug?
Thanks I'm looking forward to some help.

You can try this -
insert into hs (field1,field2,field3) select cid , sid, '1' from tmp1;
If this wont work then try please check the columns contained in both the tables. If your "hs" table contains more fields then either you have to assign them values by fetching from other table or give them some default values.

insert into hs (field1,field2,field3) select cid , sid, '1' from tmp1;
Below is the reason why the above query didn't worked
INSERT INTO SELECT requires that data types in source and target tables match
INSERT statement should contain all columns or they should have some default value assigned

Related

ERROR 1093 (HY000): Nested select on insert [duplicate]

Clearly the following is incorrect.
INSERT INTO `aTable` (`A`,`B`) VALUES((SELECT MAX(`A`) FROM `aTable`)*2),'name');
I get the value:
SQL query:
INSERT INTO `aTable` (`A`, `B` )
VALUES
(
(
SELECT MAX(`A`)
FROM `aTable`
) *2
, 'name'
)
MySQL said:
1093 - You can't specify target table 'aTable' for update in FROM clause
So, I'm trying to make a bitmap table, each row corresponds to one Bit, and has a 'map' value.
To insert in the table, I don't want to do two queries, I want to do one.
How should I do this?
No one commented on this, but since I am trying to make a bitmap, it should be * 2 not ^ 2, my mistake, please note that is why the comments often say ^ 2, it was an error in the version that the commenters read.
try:
insert into aTable select max(a)^2, 'name' from aTable;
or
insert into aTable select max(a)^2, 'name' from aTable group by B;
If you need a join, you can do this:
insert into aTable select max(a)^2, 'name' from aTable, bTable;
My "Server version" is "5.0.51b-community-nt MySQL Community Edition (GPL)"
Actually, you can alias the table on the insert. I've seen this question all over the place, but no one seems to have tried that. Use a subquery to get the max from the table, but alias the table in the subquery.
INSERT INTO tableA SET fieldA = (SELECT max(x.fieldA) FROM tableA x)+1;
A more complex example, where you have a corresponding secondary key and might be inserting the FIRST record for the corresponding secondary key:
INSERT INTO tableA SET secondaryKey = 123, fieldA = COALESCE((SELECT max(x.fieldA) FROM tableA x WHERE x.secondaryKey = 123)+1,1);
By aliasing the table, it doesn't throw the error and seems to work. I just did this while coding something, although I can't see if there area any silly syntax errors above, I would try that type of syntax.
I take it that INSERT ... SELECT isn't working? I see this in the documentation for it:
The target table of the INSERT
statement may appear in the FROM
clause of the SELECT part of the
query. (This was not possible in some
older versions of MySQL.) In this
case, MySQL creates a temporary table
to hold the rows from the SELECT and
then inserts those rows into the
target table.
Out of curiosity, which version of MySQL are you using?
I think you need to drop the "VALUES", and have a valid select statement.
see this link
I'm not particularly a mySQL guy, I use MSSQL mostly. But If you format the select statement correctly, It should work.
as soon as the Select is correct you can do this.

Mysql Incorrect date format while inserting

When was trying to insert some data to a table, I encountered this error. Incorrect datetime value: 'dweadwaed' for function str_to_date.
I was using INSERT INTO .. SELECT method to do it. The SELECT query works fine, but when I combined it with the insert query, the error happened.
SELECT id,'INVALID_DATE' FROM invoice WHERE STR_TO_DATE(paid_date, '%Y-%m-%d') IS NULL
This query returns the row 1,3.
And these 3 rows should have been inserted into the validation table using the below query. But it is showing error.
INSERT INTO validation(invoice_id,validation_message)
SELECT id,'INVALID_DATE' FROM invoice WHERE STR_TO_DATE(paid_date, '%Y-%m-%d') IS NULL
Basically the select query is same, and I am not trying to insert any date field to the DB too.
Can anyone explain the reason for this behaviour of Mysql.
I have added a SQL Fiddle for the 2 tables
We are trying to migrate data from an external application. So we cannot change the existing DB structure. Instead we are trying to identify and correct their invalid dates.
paid_date is Varchar(50)
you have inserted dweadwaed' for paid_date
Select list contains this value for paid_date so the error

MySQL INSERT INTO statement generating "Error Code: 1136. Column count doesn't match value count at row"

I am trying to create a new table from an old table so I can remove some duplicates and implement a composite primary key. I have created the new table from the old one using LIKE, so the columns should be identical.
Why does this not work?
INSERT INTO PricesNEWtest (Global_MarketCap,pkey)
VALUES ((SELECT max(Global_MarketCap), pkey
FROM Prices GROUP BY pkey));
Error generated:
Error Code: 1136. Column count doesn't match value count at row 1
The example above only has two rows so it's more legible, but in reality the tables contain 15 columns, this is the full INSERT INTO statement, which generates the same errror:
INSERT INTO PricesNEWtest (Global_MarketCap,Global_Volume24h,BTC_Dominance,Rank,Name,
Symbol,ChangePerc24h,Price,BTC_Price,MarketCap,Volume24h,DateTime,Date,pkDummy,pkey)
VALUES ((SELECT max(Global_MarketCap), max(Global_Volume24h), max(BTC_Dominance), max(Rank), max(Name),
max(Symbol), max(ChangePerc24h), max(Price), max(BTC_Price), max(MarketCap), max(Volume24h),
max(DateTime), max(Date), max(pkDummy), pkey
FROM Prices GROUP BY pkey));
I added the double brackets for VALUES because without it I get error code 1064, but I don't fully understand why the double brackets are necessary. I am grouping by the pkey field (which currently has some duplicates I want to delete), which means I need to summarize the rest of the fields. The SELECT statement works fine on its own as you can see from the screenshot.
Is there another way to do this that I could try? Or is there an easier way to remove the duplicates directly from the original table?
I am using MySQL 5.7.14
Any help would be appreciated!
You just have the wrong syntax to INSERT with a SELECT statement:
INSERT INTO PricesNEWtest (Global_MarketCap, pkey)
SELECT max(Global_MarketCap), pkey
FROM Prices
GROUP BY pkey

How to insert a value from a select statement into a table along with a hardcoded value

I have a table called ROLES with ROLEID and ROLEDESC as the columns.
I am trying to get the max ROLEID from ROLES table and store it in another table along with a hardcoded value say 'NEWROLEDESC'
The new table(viz. 'DROLES') structure is: DID and DROLEDESC where DROLEDESC is to be populate with a hardcoded value and DID is to populated with a value from the select statement : SELECT MAX(ROLEID)+1 as maxroleid FROM ROLES
I have tried :
insert into DROLES(DID) SELECT MAX(ROLEID)+1 FROM ROLES
now,the above inserts the max ID in the DID, but when I try
insert into DROLES(DID,DROLEDESC)
values ((SELECT MAX(ROLEID)+1 FROM ROLES),'NEWROLEDESC')
It doesn't work.Ofcourse the SQL grammer is not correct. Is there any way to achieve it/The correct SQL Syntax?
NOTE: I am just writing this as an experiment. I know AUTO increment would do the trick.Anything apart from that?
Reinventing AUTO_INCREMENT(MySQL)/SEQUENCE/IDENTITY(SQL Server) is not the best way to go and you may end up with incorrect values with concurent queries!
The INSERT ... SELECT syntax is:
INSERT INTO DROLES(DID, DROLEDESC)
SELECT MAX(ROLEID)+1, 'NEWROLEDESC'
FROM ROLES;
Keep in mind that if you insert multiple times to DROLES and ROLES is not chaning you will get the same value in DID column.

insert - where not exists

I would like to insert a record into a table if a record doesnt exist already exist with that domain name. The following SQL should achieve this but is getting an error.
The reason I want to do the update first is because I am doing multiple updates later in my code and need the record in my table first before doing all of the updates.
Why am I getting an error on this mySQL query?
insert into domain (name)
values ('domain.com.au')
WHERE NOT EXISTS
(
select name
from domain
where name = 'domain.com.au'
);
Both queries when split work fine but when together do not.
Let your database handle it for you. Use a unique index on name and your INSERT will fail if you try to insert a duplicate.
CREATE UNIQUE INDEX idx_name ON domain (name)
You cannot combine WHERE with INSERT clause. Use REPLACE INTO instead.
What error are you getting?
My guess would be the that the select inside the 'where not exists' is not allowed.