I am trying to upload data from cvs file to mysql. I want to filter the duplicate values of column roll.
for eg.
Table1
-----------
id name roll
1 Nirdos 4
2 krishn 2
3 shrest 2
If data is like this I want to insert first 2 rows because third row have duplicate row with second row.
Thnks In Advance
You can use the following INSERT INTO ... SELECT statement to do the insert:
INSERT INTO targetTable (id, name, roll)
SELECT t1.id, t1.name, t1.roll
FROM sourceTable AS t1
JOIN (SELECT roll, MIN(id) AS min_id
FROM sourceTable
GROUP BY roll) AS t2
ON t1.roll = t2.roll AND t1.id = t2.min_id
In case of a duplicate roll value, this query will insert the record having the minimum id value.
MySQL database provides a facility where you can make the roll column unique, and the database itself won't take duplicate input for that particular column.
The second method would be to check for duplicate entries before entering a new row and match the data with the data you are about to enter
Related
I have the following database and want to delete the red ones because they are doubouled. So I have to check every row if another row is matching by pid, price, price_old, link and shop.
But how can I check that and how can I delete it then?
Maybe an easier way would be to generate a id from the values inside each row. So if the values inside a row would be equal also the id would be equal and who have only one value to compare with the other id's.
Is that a better way? - If yes, how can I do that?
Greetings!
Do the fact you have no way for get thi distinct row you could add uniqie id using
ALTER TABLE my_table
ADD id int NOT NULL AUTO_INCREMENT
Once done you could use not in where the id are not the min grouped by the value you need for define the duplication
delete from my_table
where id NOT in ( select min(id) from my_table
group by shop, link
)
The simplest way is to run a distinct query:
select distinct pid, price, price_old, link, shop
from t;
You can create a new table using into. That is the simplest way. Because all columns are the same, MySQL doesn't offer a simple method to delete duplicate rows (while leaving one of them).
However, it is possible that your current results are generated by a query. If so, you can just add select distinct to the query. However, it would be better to fix the query so it doesn't generate duplicates. If this is the case, then ask another question with sample data, desired results (as text, not an image), and the query you are currently using.
Test this first on a test table:
DELETE t1
FROM t t1, t t2
WHERE t1.id > t2.id AND t1.price = t2.price
AND t1.link = t2.link AND t1.shop = t2.shop
AND t1.price_old = t2.price_old;
Basically you are removing the one with the highest ID if those parameters are equal
select * from
(select pid, price, price_old, link ,
row_number() over(partition by pid, price, price_old, link, shop order by pid) as rank
from my_table) temp
where temp.rank = 1
This Query will group by all the columns first and rank them. Duplicate rows will have rank > 1. It does not matter we take first or second row as both are copy of each other. We just take rows with rank 1. Rows that are not duplicate will also be having rank 1 and hence won't be neglected.
One more way to this is by using union.
select * from my_table UNION select * from my_table
I want to merge the value records as single row based on ID value.I want to display table1 value along with table2 value as single row. I want to display all in single record for the id=15.In table 1 ID is sub_id in table 2.
I want output as
Id content_value value as(15,cake,chocolate,enila,strabery)
select a.*,b.CONTENT_VALUE as multitype from album as a,album_details as b where a.ID=b.SUB_ID
Not possible to get as different columns for each value or content_value since the number of rows is dynamic.You can get the values like this by joining two tables. ie as two columns (ID and all content_value & value).
SELECT t1.ID, CONCAT_WS(',', t1.content_value, GROUP_CONCAT(t2.value)) AS contents FROM table1 t1
INNER JOIN table2 t2
ON t2.sub_id = t1.ID
GROUP BY t1.ID;
i have two tables.
First table column
value is
1,2,7.
Second table column
value is 1,2,3,4,5,6,7,8,9,10.
what i needed is i want to fetch second table values except first table values.Result should be 3,4,5,6,8,9,10.I do no what is the query for this one.Please help me.
SELECT value FROM secondtable WHERE value NOT IN (SELECT value FROM firsttable)
The standard SQL is to use NOT IN or NOT EXISTS:
select t2.*
from t2
where not exists (select 1 from table1 t1 where t1.value = t2.value);
I have 2 tables,
Table1: id,int1,int2,int3,int4,int5
Table2: integers (autoincrement),blobdata
The query I want to use is given the id I want to get the blobdata from table2 associated with the 5 integers in table1. I've only ever used one table in mysql so have no idea how to do this properly.
Is it possible?
EDIT: id is username, integers in table2 is just integers. but have not built the tables yet, so can change if need to.
select t1.id, t1.int1, t1.int2 ... t2.blobdata
from table1 t1, table2 t2
where t1.id = t2.id and t1.id = <your input id>
Assuming the auto increment integer column is the id that match the id on table1.
t2.id - or any other name you will call this column of course.
What you need is to set a foreign key in table1, which would contain the id of the blobdata you want to point to.
Take a look here: http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
Ok. Please bear with me, I suck at explaining things.
I have a database of contact information that is gathered through a form on a website. Obviously, people press submit more than once accidentally (or on purpose, but fixing is a different issue) so there are a LOT of duplicate rows in this database.
So, table1 holds contact information as such:
ID | date | unique ID code | first name, blah blah
1 stuff 20110101ba78b joe
And table2 holds related data joined by the unique ID code field, as such:
ID | data | unique ID code
1 a 20110101ba78b
2 b 20110101ba78b
So, table2 holds multiple values for each person. That's the structure of the table (and there are about a million rows in table2, so I'd rather not change the structure right now).
So my dilemma is this: I know it's easy to make a temporary table and SELECT DISTINCT(all fields), but I want to keep the unique ID field for at least 1 of the duplicate rows. If I keep the unique ID field though, it is unique for each row, even if the other data is exactly the same so SELECT DISTINCT(all fields) will not work, it will keep every row. Hopefully I explained this thoroughly. Please ask me for more information if needed.
EDIT: I'm sure I could get rid of the ID field for each table, but as far as I'm concerned it's just .... there to be there.
With the first clarification and a little reading between the lines, we can guess that it will be satisfactory to keep just the first or last entry for a given 'Unique ID Code' in Table1, where first or last means oldest or newest entry. The queries are the same except for MAX vs MIN. I'm assuming the 'date' column contains a fine enough (1 second or smaller) granularity that you don't get the same Unique ID Code twice in a time quantum; this is unlikely to be the case if the 'date' column really only contains a DATE (year, month, day) value, but probably is the case if you have a TIMESTAMP(3) and might well be the case with TIMESTAMP.
As always with SQL, build the query up in stages, nice and gently.
Find the newest entry for each Unique ID Code with multiple entries
SELECT Unique_ID_Code, MAX(date) AS Newest
FROM Table1
GROUP BY Unique_ID_Code
HAVING COUNT(*) > 1
Find the details for the Unique ID Code matching the newest entry
SELECT T1.*
FROM Table1 AS T1
JOIN (SELECT Unique_ID_Code, MAX(date) AS Newest
FROM Table1
GROUP BY Unique_ID_Code
HAVING COUNT(*) > 1
) AS M
ON M.Unique_ID_Code = T1.Unique_ID_Code AND M.Newest = T1.Date
Now the tricky stuff
What you do next depends on how much you trust the transaction support in your DBMS and how big the Table1 is, and on whether you have ON DELETE CASCADE constraints on your foreign keys, and ...
You could create a temporary table with the rows selected by the second query above (MySQL syntax, I believe; other DBMS use different notations for this).
CREATE TEMPORARY TABLE KeepTheseRows
SELECT T1.*
FROM Table1 AS T1
JOIN (SELECT Unique_ID_Code, MAX(date) AS Newest
FROM Table1
GROUP BY Unique_ID_Code
HAVING COUNT(*) > 1
) AS M
ON M.Unique_ID_Code = T1.Unique_ID_Code AND M.Newest = T1.Date;
then delete all the rows from Table1 that match the duplicate unique ID codes:
DELETE FROM Table1
WHERE Unique_ID_Code IN (SELECT Unique_ID_Code FROM KeepTheseRows);
and then reinstate the rows to be kept:
INSERT INTO Table
SELECT * FROM KeepTheseRows;
You may need to defer constraint checking while this happens, or you may need to drop the foreign key constraints while this occurs. You need to worry about activity while this operation occurs; it would be best if people were not inserting rows into Table1 while this is running. If they are modifying the table as you run, you may find that you have to do the processing several times. You should add a unique constraint to Table1.Unique_ID_Code just as soon as possible so you don't get into the mess again. (And don't forget to re-enable any deferred constraints or recreate and dropped foreign keys.)
There probably are other equivalent ways to do this; this relies only on standard (SQL-92) SQL apart from the temporary table notation.
Experiment with a copy of your production database.
This to update Table 2 to use the lowest uniqueID number for identical contact info:
UPDATE Table2
SET Table2.uniqueID = (
SELECT T1.UniqueID
FROM Table1 T1, Table1 T2
WHERE T1.unique ID < T2.unique ID
AND T1.firstname = T2.firstname
AND T1.date = T2.date
AND T1.blah, blah = T2.blah, blah
)
WHERE Table2.uniqueID = (
Select T1.UniqueID
from Table1 T1, CopyOfTable1 T2
where T1.firstname = T2.firstname
and T1.date = T2.date
and T1.blah, blah = T2.blah, blah
);
This to remove all except ONE (with the lowest uniqueID) duplicate contact info records:
delete T1
from Table1 T1, CopyOfTable1 T2
where T1.unique ID > T2.unique ID
and T1.firstname = T2.firstname
and T1.date = T2.date
and T1.blah, blah = T2.blah, blah