how to remove duplicate records? - php

I want to import excel sheet data to sql database.In database there are already have records, i want to import another records from excel and want remove duplicate records.
how can I do it
1st Table
2nd Table
I want to import 2 tables to 1 table and remove duplicate records

I'm not sure what your data looks like but this is how I would do it.
Load your new dataset into a different table.
Query your newly imported table
Left join your new data table
Filter for nulls on the old table (only select records that are NOT in old table)
Change this to an append query with "INTO X" line.
Drop the staging table.
OR you could import them both and if they are the same format you can just Union them. Select * FROM Table A UNION Select * From Table B and that will remove dupes.

Related

How to record data derived from COUNT DISTINCT query?

how to record data (into the database) derived from COUNT DISTINCT query?
For example,
Table1
AA
BB
CC
BB
BB
Then the COUNT DISTINCT query outputs the following data 131 successively based on table1.
How to automatically create a table for 131 output?
You seem to be looking for MySQL's CREATE TABLE ... SELECT Statement:
create table mydistincttable as
select ... -- your actual query goes here
MySQL creates a new table with one column for each column returned by your query, (with equivalent datatypes) and fills it with the results of the query. The names of the columns of the created table follow those returned by the query.
As you would expect, that the created table has no indexes, constraints, or the-like - because such information cannot be inferred from a query. If you want such features, you need to add them manually after the table is created.
that can be done with mysql VIEW
CREATE VIEW view_name AS your_query ;

MySQL migrate data from a table with a set field

I have a table with a set column that contains 2 languages and another column on this table containing an order.
Is it possible migrate my table and to create a record in a new table for each set item with a MySQL query?
Directly migrating records works with records that have a set column without a language combination. But if a record would contain a set of languages I am left with one record with an empty column for language. I would preferably want 2 new records inserted into my fresh table.
One for each language in the set.
INSERT INTO newtable (
newtable.oldtable_id,
newtable.language,
newtable.order
)
SELECT oldtable.id, oldtable.languages, oldtable.order
FROM oldtable
WHERE oldtable.order IS NOT NULL;
I doubt it is possible but I would still like to know if somebody might have a workaround for this since I need to migrate the data somehow.
Create a helper table with one field that lists all languages that can be in the set column. The table can be a temporary one that you drop after the migration. Then create an insert ... select ... statement in which you join the source table on the helper table using MySQL's find_in_set() function:
INSERT INTO newtable (
newtable.oldtable_id,
newtable.language,
newtable.order
)
SELECT oldtable.id, helpertable.language, oldtable.order
FROM oldtable inner join helpertable ON find_in_set(helpertable.language, oldtable.languages)>0;

MySQL Query to move/copy certain rows rather than the entire table?

I'm currently using the following to move/copy the entire table from one table to another, which works perfectly.
INSERT INTO archivedpks SELECT * FROM pks
However I want to be able to specify what rows I want to move from the pks table to the archivedpks table.
My table has an id column, I want to only move the rows with an id from 25 to 250
How would I modify the query to only work on those rows?
INSERT INTO archivedpks SELECT * FROM pks WHERE id>=25 and id<=250

Use one table to update another SQL

I have having problems updating one table from another. I want SQL to update the rows in Employees from the data in CompanyEmployees where the two EmployeeNum fields are the same. Also if an EmployeeNum exists inside of CompanyEmployees that doesn't match one in Employees then I need a new row created in Employees
I so far have tried a join for the two tables.
SELECT Employees.PhoneNum, Employees.Data,
CompanyEmployees.PhoneNum, CompanyEmployees.SystemData
FROM CompanyEmployees
INNER JOIN Employees
ON CompanyEmployees.Employees=Techs.EmployeeNum
I get the right column data in both tables but i doesnt update Employees. Do I need an INSERT or UPDATE somewhere?
How can I insert the whole row of data from CompanyEmployees into Employees where CompanyEmployees.EmployeeNum doesn't exist in Employees?
I need to do this because CompanyEmployees is only a phone directory and Employees has phone numbers and more information. But CompanyEmployees has new hires inside it that are not inside Employees.
Remember RDBMS has the R for relational. They are built for relationships.
To update you Employees table with the CompanyEmployees table you could use something like the below:
INSERT INTO Employees (columns) VALUES (SELECT columns FROM CompanyEmployees) ON DUPLICATE KEY UPDATE
You probably do not even need 'Employees' as it is a superset of 'CompanyEmployees'. I would suggest looking to merge the two tables or at least move the data into a single table.

Set column to automatically pull data from referenced table

I have one column called 'speeding' in a table containing many other columns. This column contains an integer that is foreign key to an entry on a table named Speeding. This table has columns 1-25 and an id that is referenced by the 'speeding' column from the first table.
Besides using join, is there any setting I can set on 'speeding' to make it automatically pull the associated data from the table Speeding?
You could create a view, a view is basically a SQL statement that is stored on the MySQL server and acts like a table
CREATE VIEW ViewName AS
SELECT tbl1.data, tbl2.speeding
FROM tbl1
INNER JOIN tbl2 ON tbl2.key = tbl1.key;
http://dev.mysql.com/doc/refman/5.0/en/create-view.html
You then use the view as you would use any table
SELECT data, speeding
FROM ViewName

Categories