mysql insert into table select from another database - mysql

I have a table named user in database named client. user has the structure as follows:
`fkey`(varchar(100)) | `status`(enum('1','0'))
I have another database named employees with a table named employee
It has a cloumn named emailid which has all the data I need.
I want all the emailid column data from employees.employee to be inserted in client.user at fkey column.there is another column status in client.user which needs to be set as 1 .How do I create a query for this.
This is my query but it is wrong.
insert into client.user (`fkey`,`status`) select `emailid` from employees.employee,'1';

Definitely it’s wrong syntax . It should be
Insert into client.user (fkey,status) select `emailid`, '1' from employees.employee

Related

insert multiple ID in mysql row

Is it possible to insert multiple data in a single mysql row?
Example of multiple id:
41,32,31,293,877
Yes it is possible to insert the multiple values at a time in mysql database.
suppose you have created a table of name emp. and in this table you have 1 field which is named as id.
now you just want to insert the multiple id in a table named emp , so can do this by writing following command ->
INSERT INTO emp(id) VALUES(1),(2),(3),(4),(5),(6);
Above query will insert the following given values in a table.
If you declare column to be VARCHAR().
yes you can insert multiple IDs. like
INSERT INTO table (Id) value ('41,32,31,293,877');
If you are using varchar or text data type for Id field then it is possible to enter and store with comma separated.
INSERT INTO TABLENAME (Id) VALUES ('41,32,31,293,877');
If you are inserting into a single table, you can write your query like this :
insert into table (id) values (41),(32),(31),(293),(877);

Trigger on MySQL tables with variable?

I need to create in MySQL trigger on such schema:
mail
oid id
varchar name
varchar reference
adress_mail
oid id
oid id_mail
oid id_adress
adress
oid id
varchar name
after something is inserted into adress_mail it is updating 'mail.reference' with value like every 'adress.name' for example:
I am inserting adress_mail with (1,5,6) and (1,5,7) so trigger will update 'mail.reference' with values from 'adress.name where adress.id = 6' and 'adress.name where adress.id = 7'
Q: The whole problem is how to loop on adress_mail table and get all
'adress.name' in 1 varchar and then just update 'mail.reference' with
that value?
I can setup something like var (variable) in trigger and then collect all results from select?
PS 'mail.reference' it's varchar row to improve some searching feature of application, because I don't have possibility to loop on 'adress_mail' table.
Just don't do that ! You are using an sql data base, and the main idea of it is table relations, so when you'll have to look for data from address linked to mail, perform a select with joins on address_mail.

SQL Server 2008 - Use IDENTITY field when inserting a row

My table has 3 columns ID, ISOCode and CountryName.
Column ID is an IDENTITY column.
When I insert new records into this table, I want to populate the ISOCode field - on occasion - with the same value as the ID field.
I've tried SCOPE_IDENTITY(), ##IDENTITY and IDENT_CURRENT but none of these seems to work.
Is there a way I can do this?
you could write a trigger to update the ISOCode column
In Trigger use the code below
update T set T.ISOCode=I.ID
from your_table T
join INSERTED I
on T.ID=I.ID

Copy data from one table into other table in MYSQL

INSERT INTO delete_subscriber (SubID,RandomCode,Email,FirstName,LastName,CreateDate,UpdateDate)
SELECT subscriber.*
FROM subscriber, list_sub
WHERE ListID=? AND list_sub.SubID=subscriber.SubID;
I use a select statement to get all the record and store it into the other database, the problem is , there are more column in the delete_subscriber, that means there has a column e.g. delete date, delete person that is not in subscriber table.
How can I add them in one sql statement ? Or i have to use another statement?
But if i try the latter method, the problem is delete date, delete person are not null, I can not insert the partial record using the 1st statement
INSERT INTO delete_subscriber (SubID,RandomCode,Email,FirstName,LastName,CreateDate,UpdateDate,DeleteDate,DeletePerson)
SELECT s.SubID,s.RandomCode,s.Email,s.FirstName,s.LastName,s.CreateDate,s.UpdateDate,NOW(),?
FROM subscriber s , list_sub
WHERE list_sub.ListID=? AND list_sub.SubID=s.SubID;
I can run it on sql but not php pdo? i found it not doing transaction
Change your insert statement to
INSERT INTO delete_subscriber
(SubID,RandomCode,Email,FirstName,LastName,
CreateDate,UpdateDate,DeleteDate,DeletingPerson)
SELECT s.SubID,s.RandomCode,s.Email,s.FirstName,s.LastName,
s.CreateDate,s.UpdateDate,NOW(),?
FROM subscriber s , list_sub
WHERE list_sub.ListID=? AND list_sub.SubID=s.SubID;
You can Alter delete_subscriber table set delete date, delete person default null before insert.and after populating data from select query modify again them as previous, not null.

Duplicate record in mySQL

I have a mySQL db with duplicate records, as from the attached image.
I am asking for a query to delete all duplicate records based on date + time, for all tables (foreachtables) in db
Thanks
As far I could see, you dont have autoincrement primary key or foreign key.
If you dont have tables with foreign key or relation between, first you can list all your tables. After that, you can create a temporal "mirror" of one table (for eg, autogrill).
Then you can do a:
INSERT INTO TemporalTable
SELECT DISTINCT
or a
INSERT INTO TemporalTable
SELECT Id, Date, Time FROM autogrill GROUP BY Id, Date, Time HAVING COUNT(*) > 1
.
TRUNCATE or DELETE FROM
without where and then put again your data with
INSERT INTO autogrill
SELECT * FROM TemporalTable
BE AWARE if you have primary keys doing this.
How about you create and STORED PROCEDURE for this?
DELIMITER $$
CREATE PROCEDURE `DeleteDup`()
BEGIN
-- Drops the table.
DROP TABLE bad_temp;
-- Creates a temporary table for distincts record.
CREATE TABLE bad_temp(id INT, name VARCHAR(20));
-- Selects distinct record and inserts it on the temp table
INSERT INTO bad_temp(id,name) SELECT DISTINCT id,name FROM bad_table;
-- Delete All Entries from the table which contains duplicate
-- (you can add also condition on this)
DELETE FROM bad_table;
-- Selects all records from temp table and
-- inserts back in the orginal table
INSERT INTO bad_table(id,name) SELECT id,name FROM bad_temp;
-- Drops temporary table.
DROP TABLE bad_temp;
END$$
DELIMITER ;
Please change tablename and column name to your desired schema.
so when you finish creating your STORED PROCEDURE, you can use it like this:
CALL DeleteDup();
You can export your table using this request :
SELECT * FROM autogrill GROUP BY id
Then, empty your table, and import the export you made before. I don't know another easy way to erase duplicate entries using only a single request.
One easy way to do this is to copy all the distinct records into a new table or an export. Then delete all records in the original table and copy them back in.
Export NULL if table have autoincrement an for source use alias name, example :
INSERT INTO product
SELECT NULL,p.product_sku,
p.product_s_desc,
p.product_desc,
p.product_thumb_image,
p.product_full_image,
p.product_tech_data,
p.product_publish,
p.product_weight,
p.product_weight_uom,
p.product_length,
p.product_width,
p.product_height,
p.product_lwh_uom,
p.product_url,
p.product_in_stock,
p.product_available_date,
p.product_special,
p.create_date,
p.modify_date,
p.product_name,
p.attribute
FROM product AS p WHERE p.product_id=xxx;