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

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

Related

how to remove duplicate records?

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.

how to select records from multiple database in one query?

I want to use multiple database because I have 1 database which contain 40 tables and it doesnt seem good.Thats why I want to create another database but problem is I am not able to join this databases at one query for example
I have 'core' database and it contains users table which has user_id column
second database is post database and I need get user_id from users table which is in 'core' database like below
SELECT post_message from posts where 'core'.user_id=123
is that possible?or should I stick with one database?I have also foreign key problem which is related with database relation.
If it is SQL SERVER Product, You can call the Object residing on another DB like below
SELECT * FROM [DB_NAME].[Schema_Name].[Table_NAME]
For Example:
SELECT * FROM DB1.dbo.Employee
For MySQL : Please go through the below ans
Select columns across different databases

SQL Command to delete from table where data id is in two table

I am trying to delete a row in i.e. table 2 where the id is copied from table 1 query. I want to delete this row from table 2 and also from table 1 but I am having an issue where the command I have used all work but it does not seem delete from the tables. I believe this might be because of the relationship the tables have(I used mysql workbench to make the DB design)
I used this command :
delete
from doctorsTable
where Users_idUser in (select Users_idUser from Users where idUser = 20)
This is the relation :
As mentioned, I am trying to delete the row from doctorsTable with Users_idUser=20 and automatically it would delete from Users table idUser also with 20. I have tried the above command, it seems to run but its not really deleting the rows . please help !
I think what you want to do is to delete all rows with idUser from both tables with one statement.
You can do so by joining and deleting them like:
DELETE doctorsTable, Users
FROM doctorsTable
INNER JOIN Users ON doctorsTable.Users_idUser = Users.idUser
WHERE doctorsTable.Users_idUser = 20
In the DELETE line, you specify the table(s) from which to delete matching rows.

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.

how to delete duplicate values in mysql table

I have column in mysql table (my_sale_time) of type timestamp....infact the rows value look like this
2010-12-01 14:38:07
2010-12-01 17:14:18
...
so what i need mysql query to delete those value whose date is repeated multiple times in table.....as i mentioned in sample sale_time_value.....only date is repeated with same value but time is different....so i want to get all rows, date is repeated multiple times and delete duplicate dates
The basic principle of deleting duplicate rows:
CREATE TEMPORARY TABLE tmptbl AS SELECT DISTINCT * FROM my_sale_time;
DELETE FROM my_sale_time;
INSERT INTO my_sale_time SELECT * FROM tmptbl;
You may have to specify columns and WHERE clauses (I didn't really understand your criteria).
And of course you should test-run it on a development server and don't forget to run it as a single transaction with locked tables.
If you have an auto_increment field, use this:
DELETE FROM
`mytable`
WHERE
`my_auto_increment_field` NOT IN (
SELECT
MAX(`my_auto_increment_field`)
GROUP BY
`my_sale_time`
);

Categories