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
)
Related
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
I need to add one column to my table, which would look like this:
[1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,...]
ie. sequence 1:5
I tried to use this code:
INSERT INTO table (name_of_new_column)
VALUES (1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2.....)
But what I get is this error message:
Column count doesn't match value count at row 1
How can I solve this?
Correct syntax should be :
INSERT INTO table_name (name_of_new_column)
VALUES (1),(2),(3),(4),(5)...
SqlFiddle
I want to insert a value into a table. However that value comes from that table too. And I want to check if there is a duplicate key on that table. Since this value come from that table too, the query says that a column name is ambiguous.
$result2 = "INSERT INTO estock_saldo
(items, customer_id, quantity , reference_no, size)
SELECT
items, '".$member_id."', '".$quantity[$i]."', reference_no, size
FROM
estock_saldo
WHERE id in ({$order_id[$i]})
ON DUPLICATE KEY
UPDATE estock_saldo.quantity = estock_saldo.quantity - '".$quantity[$i]."'";
$res2 = $mysqli->query($result2);
if(!$res2){ printf("Errormessage 2: %s\n", $mysqli->error); die(); }
The ambiguous come from estock_saldo.quantity. I have tried to alias the column name. However you can't do that in insert table.
Problem persist in the below shown code snippet. You can't use column alias in INSERT statement.
INSERT INTO estock_saldo
(items, customer_id, quantity AS asquan
<-- HERE
What you are trying will always have a duplicate entry, since you are inserting the same record again. Instead of INSERT statement, you actually meant to do a UPDATE like
UPDATE estock_saldo SET
quantity = quantity - '".$quantity[$i]."'"
WHERE id in ({$order_id[$i]});
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.
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.