Import/export tables in mysql - mysql

Want to import users from a table(re_user) to another table(pg_users).
Can you help me?
Thnx.
INSERT INTO `pg_users`(`id`, `fname`, `sname`, `status`, `confirm`, `login`,
`password`, `lang_id`, `email`, `date_birthday`, `date_last_seen`,
`date_registration`, `root_user`, `guest_user`, `login_count`, `active`,
`show_info`, `phone`, `user_type`, `access`, `version_message_count`, `rating`,
`about_me`, `social_data`, `date_modified`)
SELECT `id`, `fname`, `sname`, `status`, `confirm`, `login`, `password`, `lang_id`,
`email`, `date_birthday`, `date_last_seen`, `date_registration`, `root_user`,
`guest_user`, `login_count`, `active`, `show_info`, `phone`, `user_type`,
`access`,`version_message_count`, `rating`, `about_me`, `social_data`,
`date_modified` FROM `re_user` WHERE 1

Based on what you disclosed in your comments, that you want to insert data into a table by pulling it from a table in another database, you must be specific as to which database this information is coming from, and going to. Update the DESTINATION_SCHEMA_NAME and the SOURCE_SCHEMA_NAME in my example below and that should resolve the issue. Also, if you are copying everything, there is no need for the where clause in your select statement.
INSERT INTO `DESTINATION_SCHEMA_NAME.`pg_users`(`id`, `fname`, `sname`, `status`, `confirm`, `login`,
`password`, `lang_id`, `email`, `date_birthday`, `date_last_seen`,
`date_registration`, `root_user`, `guest_user`, `login_count`, `active`,
`show_info`, `phone`, `user_type`, `access`, `version_message_count`, `rating`,
`about_me`, `social_data`, `date_modified`)
SELECT `id`, `fname`, `sname`, `status`, `confirm`, `login`, `password`, `lang_id`,
`email`, `date_birthday`, `date_last_seen`, `date_registration`, `root_user`,
`guest_user`, `login_count`, `active`, `show_info`, `phone`, `user_type`,
`access`,`version_message_count`, `rating`, `about_me`, `social_data`,
`date_modified` FROM `SOURCE_SCHEMA_NAME`.`re_user`

This is how you can do that.
Command Line
Import / Export for single table:
Exporting table schema
-> mysqldump -u your_user_name -p your_database_name table_name > test.sql
This will create a file named test.sql and creates table sql command to create table table_name.
Importing data into table
-> mysql -u your_user_name -p database_name table_name < test.sql
Make sure your test.sql file is in the same directory, if not navigate through the path and then run the command.

Related

keep getting error trying to add records into table

mysql>
insert into `customers`
( `customerNumber`,
`customerName`,
`contactLastName`,
`contactFirstName`,
`phone`,
`addressLine1`,
`addressLine2`,
`city`,
`state`,
`postalCode`,
`country`,
`salesRepEmployeeNumber`,
`creditLimit`)
values
(497,
'SomeName',
'LastNameSample',
'800-555-1234',
'123 Main St',
'Apt 41',
'Some Town',
'CA',
'96024',
1002,
20000);
Error:
ERROR 1136 (21S01): Column count doesn't match value count at row 1
above is what I have written in order to add a record into a Customer table but I keep getting an error. help me, please?

Is it possible to insert in mysql a combo of words and variable?

Trigger definition:
BEGIN
INSERT INTO `user` (`ID`, `Username`, `Password`, `Online`) VALUES
(NEW.ID, "store_"NEW.ID , 'blablabla', 0);
END
In this trigger I have to insert as username something like store_(id of the new store).
Is it possible to do this and if yes how?
There are 2 ways to concat/append string in SQL
a. Use + operator to join the string
b. Use Concat function.
By using + operator the query would be like below.
BEGIN
INSERT INTO `user` (`ID`, `Username`, `Password`, `Online`) VALUES
(NEW.ID, "store_" + NEW.ID , 'blablabla', 0);
END
By using Concat() function the query would be like below.
BEGIN
INSERT INTO `user` (`ID`, `Username`, `Password`, `Online`) VALUES
(NEW.ID, Concat("store_",NEW.ID) , 'blablabla', 0);
END
Recommended option is use 'Concat' function as syntax is easier to follow, and the code looks cleaner
Use concat function :
BEGIN
INSERT INTO user (ID, Username, Password, Online)
VALUES
(NEW.ID, CONCAT('store_',NEW.ID), 'blablabla', 0);
END

Sakila -> insert into Film ( mysql WorkBench )

I'm trying put a new row in table sakila. film, but I can't see it in the table, I think that not exist, but when execute this statement Works correctly, I don't know what happened
INSERT INTO `sakila`.`film`
(
`title`,
`description`,
`release_year`,
`language_id`,
`original_language_id`,
`rental_duration`,
`rental_rate`,
`length`,
`replacement_cost`,
`rating`,
`special_features`,
`last_update`)
VALUES( 'GUSANO' ,'GUSANO' ,2006,1,1,3,4.99,4 ,19.99, 'G','Trailers','2006-02-15 04:34:33.0');
Result :
21:52:44 INSERT INTO `sakila`.`film` ( `film_id`, `title`, `description`, `release_year`, `language_id`, `original_language_id`, `rental_duration`, `rental_rate`, `length`, `replacement_cost`, `rating`, `special_features`, `last_update`) VALUES( 10010, 'GUSANO' ,'GUSANO' ,2006,1,1,3,4.99,4 ,19.99, 'G','Trailers','2006-02-15 04:34:33.0') 1 row(s) affected 0.015 sec
Image insert :
Image select :

Uknown Column new in FIELD LIST Mysql Trigger

I have the following trigger which is supposed to insert on a trigger table after insert. I keep on getting the following error : Unknown Column Field in List
Below is my trigger :
DROP TRIGGER IF EXISTS `trgr_client_statement_report`;CREATE DEFINER=`uniquelo`#`localhost` TRIGGER `trgr_client_statement_report` AFTER INSERT ON `statement` FOR EACH ROW INSERT INTO trgr_statement
( `amnt_cr`, `amnt_dr`, `approved`, `clnt_id`,`date_added`,`days`, `description`,
`discount`,
`id_2`,
`invoiced`,
`invoice_no`,
`job_card_id`,
`payment_date`,
`payment_status`,
`pymnt_code`,
`pymnt_mthd`,
`qty`,
`rcpt_no`,
`status`)
VALUES
( NEW.`amnt_cr`,NEW.`amnt_dr`,NEW.`approved`,NEW.`clnt_id`,NEW.`date_added`,NEW.`days`,NEW.`description`,NEW.`discount`,NEW.`id`,NEW.`invoiced`,NEW.`invoice_no`,NEW.`job_card_id`,NEW,`payment_date`,NEW.`payment_status`,NEW.`pymnt_code`,NEW.`pymnt_mthd`,NEW.`qty`,NEW.`rcpt_no`,NEW.`status`)
There are some Syntax error in fieldlist. You must use NEW.field not NEW,field : NEW,payment_date
DROP TRIGGER IF EXISTS `trgr_client_statement_report`;
CREATE DEFINER=`uniquelo`#`localhost` TRIGGER `trgr_client_statement_report` AFTER INSERT ON `STATEMENT` FOR EACH ROW INSERT INTO trgr_statement
( `amnt_cr`, `amnt_dr`, `approved`, `clnt_id`,`date_added`,`days`, `description`,
`discount`,
`id_2`,
`invoiced`,
`invoice_no`,
`job_card_id`,
`payment_date`,
`payment_status`,
`pymnt_code`,
`pymnt_mthd`,
`qty`,
`rcpt_no`,
`status`)
VALUES
( NEW.`amnt_cr`,NEW.`amnt_dr`,NEW.`approved`,NEW.`clnt_id`,NEW.`date_added`,NEW.`days`,NEW.`description`,NEW.`discount`,NEW.`id`,NEW.`invoiced`,NEW.`invoice_no`,NEW.`job_card_id`,NEW.`payment_date`,NEW.`payment_status`,NEW.`pymnt_code`,NEW.`pymnt_mthd`,NEW.`qty`,NEW.`rcpt_no`,NEW.`status`)

MySQL insert into values (select)

I am trying to insert value into mysql 5.1 table using the following statement
INSERT INTO `bp_measurements`
(`id_patient`, `time`, `location`, `systolic`, `diastolic`)
VALUES (
'2',
'2015-12-26 13:19:35',
(SELECT `id` FROM `gps_locations` WHERE `lon` = 20.40930 AND `lat` = 48.94990`),
'110',
'70'
)
But I am getting syntax error MySQL server version for the right syntax to use near '`),'110','70')' at line 2 (sorry for not putting this into code but it is not working because of apostrophes used). What am I doing wrong? I have tried some other solution from stackoverflow but I cant get them working
Thanks in forward
EDIT:
this is the correct version of my statement
INSERT INTO `bp_measurements`
(`id_patient`, `time`, `location`, `systolic`, `diastolic`)
VALUES (
'2',
'2015-12-26 13:19:35',
(SELECT `id` FROM `gps_locations` WHERE `lon` = 20.40930 AND `lat` = 48.94990),
'110',
'70'
)
both these and accepted answer works so feel free to use any of them
Try this syntax
Add the constant values to the select column list in order of insert column list
INSERT INTO `bp_measurements`
(`id_patient`,
`time`,
`location`,
`systolic`,
`diastolic`)
SELECT '2',
'2015-12-26 13:19:35',
`id`,
'110',
'70'
FROM `gps_locations`
WHERE `lon` = 20.40930
AND `lat` = 48.94990
Modified Insert statement as:
INSERT INTO bp_measurements(id_patient, time, location, systolic, diastolic) (SELECT '2' AS id_patient,'2015-12-26 13:19:35' AS time,id AS location,'110' AS systolic,'70' AS diastolic FROM gps_locations WHERE lon = 20.40930 AND lat = 48.94990)