I have a table used by cms which holds data for several languages.
If i'd like to add another language i would have copy existing pages(all of some language) only with a change value in column 'lang'.
How to copy row and change value of one column which will put to the same table?
Thanks
INSERT INTO foobar (lang,text)
SELECT 'de',text
FROM foobar
WHERE lang='en';
You'd do this with a statement like:
insert into <table_name> (language, value1, value2)
select 'new_language', value1, value2
from <table_name>
where language = 'old_language'
as per the manual: http://dev.mysql.com/doc/refman/5.5/en/insert-select.html
Related
I am trying to copy data from one table to another with an additional value.
from table1 I need to copy two column(field) values such as ERNO and ENAME to table2.
Also need to update or add ECNO(column or field).
Note: I am using MySQL.
Not only one field ECNO, also need to add more field while copying data from one table to another.
ECNO field is int datatype.
Follwing query I have used for that. But it doesn't work
INSERT INTO TABLE2 (ECNO, ERNO, ENAME) values (1, select ERNO, ENAME from TABLE1)
Any Suggestion how to do this in proper way.
Use INSERT INTO...SELECT syntax:
INSERT INTO TABLE2 (ECNO, ERNO, ENAME)
SELECT 1, ERNO, ENAME
FROM TABLE1
Use like this:
INSERT INTO tbl2 (col1, col2.....)
SELECT tbl1.col1,tbl1.col2
FROM tbl
Is it possible to direct insert all columns to 1 column for blob file. Or do I need to outfile first the table so that I can upload it as blob. If ever possible here is my imaginary query, something like this:
insert into blob("file") values ('va1','va2','val3')
Any suggestions/comments thanks in advance.
In mysql, what you're thinking of is concat_ws. This allows you to combine the results from multiple columns.
INSERT INTO mytable(value1)
SELECT CONCAT_WS(',', value1, value2, value3)
FROM table
LIMIT 1;
However, in this case, you don't use the VALUES syntax, you simply run your query.
Here is a fiddle
http://sqlfiddle.com/#!9/57abb/1/0
We have database table 'field_data_body' and we are using 'insert into ... select from' mysql command to insert data into 'field_data_body' from another table.
Table structure:
In our database table, delta column is used to differentiate same record. For example,
In above exmaple, both row has same data except different value of delta.
How can we set delta while inserting data into database table?
I have search in google and some other questions in stack exchange but did not find solutions.
Thanks in advance.
You could use 2 queries for that.
insert into tablename (col1, col2, ..) Values (value1, value2, ..);
insert into tablename (delta) Values (value) where entity_type = 'node';
Is there a way to use a MySQL INSERT similar to the following:
INSERT INTO doc_details SELECT * FROM doc_details WHERE dd_id = 1
This doesn't work because the primary key is being repeated and it can get very long-winded expanding the columns out.
The purpose of this is to duplicate rows in the same table which will get modified later, retrieving the last_insert_id for the new record. So ideas for other ways to do this would be appreciated too.
Thanks.
Simply name the columns you want to duplicate and omit the primary key:
INSERT INTO doc_details (col1, col2, col3)
SELECT col1, col2, col3
FROM doc_details
WHERE dd_id = 1
I'd suggest you to make ID field with AUTO_INCREMENT option, then use NULL values when inserting -
INSERT INTO doc_details(id, column1, column2)
SELECT NULL, column1, column2 FROM doc_details WHERE dd_id = 1;
In this case old ID will be changed with new ones.
You can depend on temporary table to copy from old record and omitting the key field value.
You have to use at least one named column, i.e. the key field name, to omit its repeating values.
See the following example:
CREATE TEMPORARY TABLE tmp SELECT * from doc_details WHERE dd_id = ?;
ALTER TABLE tmp drop pk_field_name_here; -- drop the key field for not repeating
INSERT INTO doc_details SELECT 0, tmp.* FROM tmp;
DROP TABLE tmp;
You can observe that no other filed names are used but the key field name to omit it's value.
You can also refer to my answer to a similar posting at: Mysql: Copy row but with new id.
Thanks for the answers. Really appreciated. Because most answers specify the column, this led to some extra research that said 'wildcards cannot be used in INSERT statements. Select, Modify and insert into the same table
I managed to solve this in my application with a separate SELECT then the INSERT with the columns expanded with a Perl map function:
SELECT * FROM doc_details WHERE dd_id = 1
Then in Perl, with the row as a hash reference in $data:
$data->{'dd_id'} = 0;$columns = join(',', map {$_ .'='. $dbh->quote( $data->{$_} ) } keys %{$cdh} );
Does the trick nicely - it copies the row regardless of changes to the column structure/order as long as the auto_increment column is maintained.
I know it's not a pure SQL solution - although Ravinder provided one that was.
Thanks to all!
I find that there are only after and instead of triggers in sql server. And it is illegal to modify the values in the inserted pesudo table. Then my problem occurs: If I want to check the data which is going to be inserted into my table, and when the data violates my constraints I should modify these values to default values, how to do it ? How about updateing the values after inserted ? However, if there's no primary key or colum which is unique in my table, how can I locate the row just inserted and then update it ?
Basically, with an INSTEAD OF INSERT trigger, you can achieve what you're looking for - just read out the data from the INSERTED pseudo table, modify it, and insert it into the table
So your trigger would look something like this:
CREATE TRIGGER YourTrigger ON dbo.YourTable
INSTEAD OF INSERT
AS
SET NOCOUNT ON
-- do the INSERT based on the INSERTED pseudo table, modify data as needed
INSERT INTO dbo.YourTable(Col1, Col2, ....., ColN)
SELECT
Col1, 2 * Col2, ....., N * ColN
FROM
INSERTED
Of course, you could also add e.g. checks in the form of WHERE clause to that SELECT .... FROM INSERTED statement to e.g. ignore certain rows - the possibilities are endless!