How do I move data from one table into another in MySQL? - mysql

I have the following query which pulls 1000 rows of data:
select barcode, type from collections where user = 'myusername'
I need to move this data to my new collectors_collections table. The user column is now labeled username.
What is the quickest way to do this? A query of some kind or some other way? I am using the latest MySQL Workbench.

You could try running this query:
INSERT INTO
collectors_collections(barcode, type, username)
SELECT
barcode,
type,
`user`
FROM
collections
WHERE
`user` = 'myusername';

Well, a simplistic approach which I would use it to just cut and paste the results of your selection into a text editor. And perform some simple search/replaces to create insert statements for your new table. Then just paste the new insert statements into your mysql client.
I assume you are not wanting this functionality with a running program.

Related

mySql table data can't be viewed using SELECT statement only give empty result

In my test project MySQL table is created to store data (State, District, Area) I declare these columns as text. when submit form data saved to database. but when try to review using SELECT statement with WHERE clause it give empty result SELECT * FROM `users` WHERE district='districtName';. please advice what is the error.
can you share more details of database
like name of database and how many records in the database

transfer data from 1 table to another in the same database

Is this the right syntax:
INSERT INTO stock (Image)
SELECT Image,
FROM productimages
WHERE stock.Name_of_item = productimages.number;
SQL Server Management Studio's "Import Data" task (right-click on the DB name, then tasks) will do most of this for you. Run it from the database you want to copy the data into.
If the tables don't exist it will create them for you, but you'll probably have to recreate any indexes and such. If the tables do exist, it will append the new data by default but you can adjust that (edit mappings) so it will delete all existing data.
I use this all the time and it works fairly well.
INSERT INTO bar..tblFoobar( *fieldlist* )
SELECT *fieldlist* FROM foo..tblFoobar
This just moves the data. If you want to move the table definition (and other attributes such as permissions and indexes), you'll have to do something else.
The query logic that you are trying appears to be not correct (the query itself is buggy).
Assuming you have the correct query for the above logic and what you are trying is to insert new rows into the table stock by selecting a column from productimages table with a matching record as stock.Name_of_item = productimages.number
The above logic will add redundant data in to the table.
You perhaps looking to update instead of insert, something as -
update stock s
join productimages p on p.number = s.Name_of_item
set s.Image = p.Image

MySQL - Automate a view update

I'd like to know if it is possible to create an SQL function that will automatically update my View and add to it new tables in my DB.
My DB consist of multiple tables (same data structure) and are named as follow "MM_DD", now I would like to create a VIEW that joins all this data ( pretty simple , see query below) but I wish to automate the process so every time a new table is added the view will update.
CREATE OR REPLACE VIEW `viewTable` AS
select *,
md5(CONCAT(`columnA`,`columnB`)) AS myPK_id
from `05_06`
union all
select *,
md5(CONCAT(`columnA`,`columnB`)) AS myPK_id
from `05_08`
ect...
What I am doing at the moment is using PHP every time a table is added. It loops through the tables and create / update the view.
select * from information_schema.tables WHERE table_name LIKE '%05%'
Now that I have an array of table names -> create my Query string -> replace view...
Is this possible to do this in SQL?
If so, what is the correct approach?
Write a stored procedure to rebuild your view. You'll need to use what MySQL internally calls "prepared statements" so you can use string manipulation.
You'll still use your SELECT ... FROM information_schema.tables query to drive this stored procedure.
Once you get this working, you can embed it in a mySQL event, and arrange to run it automatically. For example, you could run it at the same time late at night.
Or, you can invoke the stored procedure immediately after you create one of these tables.

MySQL: Transfer duplicate records to another table, during INSERT `tablename` SELECT * FROM `data source`

I'm making a transition from MS Access into MySQL.
With Access, I can copy and paste data between tables, any duplicates will be automatically placed in the table paste_errors. How can I accomplish this in MySQL?
You can try creating a Trigger. Check out this SO post which will outline two ways to do it.

Add a custom INSERT for MySqlCommandBuilder to use

I'm connecting and querying a database, and I'm getting data from two tables like this:
SELECT * FROM tblOne, tblTwo
I'm binding some textboxes to fields in the database, and added a BindingNavigator for easy paging, and inserting data. Therefore I planned to use MySqlCommandBuilder to automatically build the INSERT command.
But, of course MySqlCommandBuilder can't generate a INSERT command based on a query with two tables.
Is there a easy way to fix this? Like adding a custom INSERT command?