I am getting problem while adding multiple rows at the same time
I have a mysql table in which Replace multiple rows of the same id
Suppose I have 2 columns
1) offer_id
2)categories
By using php script I am replacing all the rows day by day,So I add unique key for the offer_id and categories
but the problem is that,when there are the two values containing
1) offer_id=2 and categories = ecomm
2) offer_id=2 and categories = market
my query will run as follows like
REPLACE INTO `affiliate_offer_get_categories_icube` (`offer_id`, `net_provider`, `categories`) VALUES
(2, 'icube', 'Marketplace');
REPLACE INTO `affiliate_offer_get_categories_icube` (`offer_id`, `net_provider`, `categories`) VALUES
(2, 'icube', 'Ecoommerce');
In above statements I have to add two rows of same 'offer_id' but different 'categories'.
but I am getting in result only one row(I have to add values of both categories.)
Sounds like you need your unique index to span over both columns. Drop the unique index you have and create a new one with
CREATE INDEX idx_whatever_name ON your_tablename (offer_id, categories);
Related
I'm trying to write a SQL statement that replace instead of update.
The columns of my table look like that
(id
block
region
login
password
email
business
firstname
name
version
updatable
bodyshop_id
mac
register_date
lastvisite_date
enum_test
address1)
and when I run a statement like this:
REPLACE INTO `users` (`login`, `firstname`, `region`, `address1`, `enum_test`, `block`, `id`) VALUES ('Samira GO', 'Samira', 'all', 'lmklm', '1', '0', '2')
Samira have the id number two. (target of the replace ;) )
The person with the id number one is drop by the request.
(The primary id key of the table is id+login+email)
(When I ask this request to SQL it told me that 3 lines are affect)
If you want to ask, id, login, or email are some primary value, so I don't understand how it would be able to change some value with another id or login
From the MySQL REPLACE doc:
The REPLACE statement returns a count to indicate the number of rows affected. This is the sum of the rows deleted and inserted. If the count is 1 for a single-row REPLACE, a row was inserted and no rows were deleted. If the count is greater than 1, one or more old rows were deleted before the new row was inserted. It is possible for a single row to replace more than one old row if the table contains multiple unique indexes and the new row duplicates values for different old rows in different unique indexes.
So, it sounds like one row was inserted and two rows were deleted.
Examine your table definition and see if there are any UNIQUE indexes other than the PRIMARY KEY. Note also that while you say the primary key is id, login, email, your query doesn't specify email. If two rows existed that matched id and login but had different email, they may have both been deleted.
You may also consider that what you wanted to do is an INSERT ... ON DUPLICATE KEY UPDATE instead of a REPLACE. REPLACE functions more like a combined DELETE then INSERT.
I have a table with 2 columns, userid and messageid. I am updating it automatically through a php script, but I can't get a working insert statement. I don't mind if there are duplicates of userid, or duplicates of messageid (in fact there should be duplicates of both), I just don't want any duplicate of the same combination of userid and messageid. Is there any way to write a query that will do this for me, or do I have to handle it at the php level?
I've probably tried 20 different queries that I found on here and google, but have not gotten it right. This was the last thing I tried:
INSERT INTO interests_join (userid, interestid)
VALUES (1, 4)
WHERE NOT EXISTS
(SELECT userid, interestid FROM interests_join WHERE userid = 1 AND interestid = 4)
You can add a UNIQUE KEY, sql will refuse to insert a new row that is a duplicate of an existing one.
ALTER TABLE `interests_join` ADD UNIQUE `row` (`userid`, `interestid`);
Then you'll have to check from PHP if the query was successful or not (error #1062). You can't apply the key if there are duplicate rows, you have to remove them first .
I've an 'orders' table structure like this which contains 100,000 records:
date orderid type productsales other
01-Aug-2014 11 order 118 10.12
01-Aug-2014 11 order 118 10.12
18-Aug-2014 11 order 35 4.21
22-Aug-2014 11 Refund -35 -4.21
09-Sep-2014 12 order 56 7.29
15-Sep-2014 12 refund -56 -7.29
23-Oct-2014 13 order 25 2.32
26-Oct-2014 13 refund -25 -2.32
Now, what I want to achieve is to delete those duplicate row from my table where the orderid, type, productsales and other columns values are same to each other and keep only one row (look at the first two records for the orderid of 11).
But if the 'orderids' are same for the two records of the same 'type' of order, but the 'productsales' and 'other' columns values are different then don't delete those rows. I hope I clarified my point.
I'm looking for a mysql delete query to perform this task.
You should add an id column. If you don't want to use a temp table, you could probably do something like this (I have NOT tested this, so...):
ALTER TABLE 'orders'
ADD COLUMN 'id' INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY Id(id)
DELETE
FROM orders INNER JOIN
(
SELECT TOP 1 id
FROM orders
WHERE COUNT(DISTINCT date,orderid,type.productsales,other) > 1
) dupes
ON orders.id = dupes.id
May be its duplicate question to this: MySql: remove table rows depending on column duplicate values?
You can seek for the answer there.
The solution there specify that adding unique index on your possible duplicate columns with IGNORE keyword will remove all duplicates row.
ALTER IGNORE TABLE `table` ADD UNIQUE INDEX `name` (`col1`, `col2`, `col3`);
Here I also want to mention some points:
unique index does not make change in row if any columns(from index, like here 3 columns) have null as value. Ex: null,1,"asdsa" can be stored twice
same way if you have single column in unique index then multiple rows with null values(for that column) will remains in table
IGNORE keywords id depreciated now, it will not work after MySQL 5.6(may be). Now only option is to create new table by a query like this:
CREATE TABLE <table_name> AS SELECT * FROM <your_table> GROUP BY col1,col2,col3;
After that you can delete <your_table> and rename <table_name> to your table.
Here you can change the column list in Group By clause according to your need(from all columns to one column, or few columns which have duplicate values together).
The plus point is, it will work with null values also.
A really easy way to do this is to add a UNIQUE index on the 3 columns. When you write the ALTER statement, include the IGNORE keyword. Like so:
ALTER IGNORE TABLE orders ADD UNIQUE INDEX idx_name (orderid, type, productsales, other);
This will drop all the duplicate rows. As an added benefit, future INSERTs that are duplicates will error out. As always, you may want to take a backup before running something like this...
I hope this can help you.
try this.
create temp table such as temp and stored unique data,
SELECT distinct * into temp FROM Orders
then delete records of orders table table as
DELETE FROM orders
after deleted all records insert records temp into records.
INSER into RECORDS SELECT * FROM TEMP DROP TABLE TEMP
If you have completely duplicated rows, and you want to do this in SQL, then perhaps the best method is to save the rows you want in a temporary table, truncate the table, and insert the data back in:
create temporary table temp_orders as
select distinct *
from orders;
truncate table orders;
alter table orders add orderid int not null primary key auto_increment;
insert into orders;
select *
from temp_orders;
Oh, look, I also added an auto-incrementing primary key so you won't have this problem in the future. This would be a simpler process if you have a unique key on each row.
I got this query in a loop:
while ($row = mysql_fetch_array($query)) {
INSERT INTO restaurant_views (date, restaurant_id, views) VALUES ('{$current_date}', '{$row['id']}', 1) ON DUPLICATE KEY UPDATE views=views+1
}
What this code should do is to update the column views inside the table restaurant_views.
The problem is that it doesn't check if date and restaurant_id is duplicate, it just checks if the primary column in the database is duplicate.
Here is a screenshot of the table:
As you can see, both date and restaurant_id is unique, but what this code give me is this:
The query was looped 7 times, as you can see in the views. This is wrong.
What I want is 7 different rows with 1 view in each of them.
If you want uniqueness on the restaurant id/date then you need a unique index/constraint on those columns:
create unique index idx_restaurant_views_2 on restaurant_views(restaurant_id, date)
Then your code should work.
I've been fighting to get the SQL right on this insert statement.. here's what I'm trying to do:
I have new empty table that has a 1-to-1 relationship with another table. Since all the other columns have default values I only want to insert the primary key column as new rows from the old table. Table names are dealer and dealer_nav and the fields are id and dealer_id respectively.
Thanks!
INSERT INTO dealer_nav(dealer_id) SELECT id FROM dealer (or vice versa, I'm not sure which of 2 tables is new and which is old)