#1136 - Column count doesn't match value count at row 1 - mysql

I am trying to insert ID field from one table to another using below query:
INSERT INTO `srikprag_db`.`acbalance`
SELECT `id` FROM `srikprag_mlm`.`member_table`
Error is showing:
#1136 - Column count doesn't match value count at row 1
What is the reason for this error?

You did not define the destination column on where the values from the SELECT statement will be saved, eg.
INSERT INTO srikprag_db.acbalance (ID) -- <<== destination column
SELECT id
FROM srikprag_mlm.member_table
probably you want to manipulate records across database.

The problem is with your query you are not assigning any value to the column. You have 1 column with zero value.

SELECT `id` FROM `srikprag_mlm`.`member_table`
returns a result set with only 1 column (id).
The acbalance table probably has more than 1 column.

Related

ERROR 1136 (21S01): Column count doesn't match value count but am giving same number of values with same data type

I am inserting the same number of values as of my number of columns with correct data type but still, it is showing, column count does not match.
All the columns are here in this link
INSERT INTO map_city VALUES
-> ('IN',744101,'marine jetty','andaman & nicobar islands','south andaman','portblair',11.6667,92.75,3,132,51,57,5,163,55,2770,4.76534296,2.057761733,5.884476534,61.92561093,8.605560944,763.0978608);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
I have also tried, double quotes but still not working.
Try inserting values into columns with respect to their fields, like
INSERT INTO map_city (country_code, pin_code, place_name, ) VALUES ('IN',744101,'marine jetty');
Please Try NULL value for the insert into the auto increment column (id), I think that will fix it.
OR
INSERT INTO tablename (col1,col2,col3) VALUES ('val1','val2','val3')

how to copy data from one SQL column table to another SQL column table

Need assistance on the following. How can I copy data from one SQL column table to another sql column table?
I have the following tables
dbo.Thecat62 and dbo.thecase6
inside dbo.Thecat62 , I need to copy the column Work_Order_Job_Start_Date values to dbo.thecase6 column Job_Start_Date. Currently there are null value in the Job_Start_Date column in dbo.thecase6.
I have tried using the following command
INSERT INTO dbo.thecase6 (Job_Start_Date)
SELECT Work_Order_Job_Start_Date
FROM dbo.thecat62
but received the error Cannot insert the value NULL into column 'CaseNo', table 'Therefore.dbo.TheCase6'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Any help will be great!
Thanks!
Because on this table Therefore.dbo.TheCase6 for CaseNo you have specify Not NULL Constraints
something like this
CaseNo int NOT NULL
But you did not select the CaseNo column from the dbo.thecat62 table, so you are explicitly trying to insert nulls into a non-nullable column.
You just need to select the CaseNo column, as well, presuming it does not contain any nulls in teh source table.
INSERT INTO dbo.thecase6 (Job_Start_Date,CaseNo)
SELECT Work_Order_Job_Start_Date,CaseNo FROM dbo.thecat62
The error says it has a column CaseNo which doesn't allow NULL.
Cannot insert the value NULL into column 'CaseNo', table 'Therefore.dbo.TheCase6';
You are inserting rows in the new table which will have just 1 column filled and rest of the columns will be empty
Either
Alter the table in which you are inserting the data and allow the column to allow null values.
Or
if you don't want to allow null values, update the null values to some default values.

MySQL column with duplicate values

I have a string column which has duplicate values, for example
row 1 column "desc" has value -> "stack-overflow stack-overflow"
I want to update the column to "stack-overflow"
any idea how to build a query for that?
duplicate record must be symmetric and should be repeated twice only then following will do the trick
select
left
(
'stack-overflow stack-overflow',
length('stack-overflow stack-overflow')/2
)

update unique indexed column in mysql

I have a unique indexed column A with integer in it. I'd like it to increment by 1, in Oracle I do: update table set A=A+1 and it worked. But in mySQL, it give me the following error:
- MySQL Database Error: Duplicate entry '2' for key 1. I have three rows in the table with value: 1, 2 and 3 individually. I know why it gives me this error. But how do I solve this problem? Thanks.
You receive this error because your UPDATE TABLE SET A = A + 1, when updating the first ROW from 1 to 2 (1+1), it will get conflict with your second ROW, because there already is a ROW with ID = 2.
You have to do it descender from the last ROW to the first, do you have to alter your query to this:
UPDATE TABLE SET ID = ID + 1 ORDER By ID DESC;
The DESC clause will make the updates from the bottom of your table, so it won't find any duplicate ID in his path...
You can do this using an ORDER BY:
update table
set A=A+1
order by A desc

Copy from one column to another (different tables same database) mysql

Hi I would like to copy entire contents from column Item under table IName to column Name under table Item both belonging to the same database.
I am giving the following query but it throws the error saying that the subquery returned more than one records. (There are around 600 records)
Insert into Item set name = (Select Item from IName)
Thanks
INSERT INTO Item (Name)
SELECT Item
FROM IName
When you want to insert into a single-column* table, INSERT works either with:
INSERT INTO table (column)
VALUES (value1),(value2), ... (valueN) ;
or with:
INSERT INTO table (column)
SELECT a_column
FROM a_table
--- optional (multiple) JOINs
--- and WHERE
--- and GROUP BY
--- any complex SELECT query
(OK, the above can work with a multiple-column table, too, as long as all the other - not explicitely stated in the INSERT statement - columns have been defined with a DEFAULT value or with AUTO_INCREMENT.)
The INSERT ... SET syntax is valid in MySQL only and can be used only when you want to insert one row exactly:
INSERT INTO table
SET column = value1 ;
is equivalent to:
INSERT INTO table (column)
VALUES (value1) ;
INSERT INTO Item (name)
SELECT Item FROM IName
Link
INSERT INTO table_one (column1) SELECT column2 FROM table_two
See MySQL Ref