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
Related
I am fairly new to SQL and im not sure if this is possible, but as the title suggests I want to combine the results of a query into one "entity" that I can then later insert into another table.
Here is an example:
SELECT COLUMN1
FROM TABLE1
Let's say the output is this:
COLUMN1
-------
data1
data2
data3
What I want to do is to take the result of this query and turn it into this:
data1,data2,data3
Which I can then insert into another table as one row like this:
INSERT INTO TABLE2(RESULT)
VALUES ('data1,data2,data3')
You can use GROUP_CONCAT to get a list of all your column values:
SELECT GROUP_CONCAT(COLUMN1 SEPARATOR ',') FROM TABLE1
demo: http://sqlfiddle.com/#!9/3cf4ecc/5/0
To directly INSERT the result of this SELECT you can use the following:
INSERT INTO TABLE2 SELECT GROUP_CONCAT(COLUMN1 SEPARATOR ',') FROM TABLE1
demo: http://sqlfiddle.com/#!9/466869/1/1
You can also use a VIEW to keep your tables normalized:
CREATE VIEW V_TABLE1 AS SELECT GROUP_CONCAT(COLUMN1 SEPARATOR ',') FROM TABLE1;
The advantage of a VIEW is that it is always generated from the current values of your table.
... but be careful: As marc_s already mentioned in the comments, it is not recommended to store multiple values as a list in a single column. You should always design normalized databases.
What you need is an aggregate function such as string_agg in postgresql , simply as SELECT string_agg(COLUMN1, ',') FROM TABLE1;
Certainly you can combine it with INSERT, i.e INSERT ...SELECT.
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';
For example i have table with a different field names(column), lets say 5 columns and all of them are empty. And i wanted to insert data in one specific column. Is it possible? I'm looking for example of this, but unlucky to find one. Most of insert into statements examples required all columns to be filled. If possible, can you give me the correct syntax? I'm sorry if i'm lacking research or it's already been asked, it's ok if you will redirect me to the link.
If you want insert on column3, leaving empty the other:
INSERT INTO table_name (column1,column2,column3,column4,column5)
VALUES ("","","VALUE","","");
The other part of program would UPDATE the other columns:
UPDATE table_name
SET column1=value1,column2=value2,column4=value4,column5=value5
WHERE some_column=some_value;
The documentation on how to construct an INSERT INTO statement is here: INSERT INTO Statement (Microsoft Access SQL).
But basically, you just need to be explicit about which columns you want to insert values for, and omit the other ones, like this:
INSERT INTO table (colname) VALUES ('colvalue')
What happens to the fields you omit? The documentation says:
When you do not specify each field, the default value or Null is inserted for missing columns.
I'm looking for a way I can duplicate all the rows in my database, I tried exporting it and then importing but I get the duplicate key error.
The reason is purely for testing purposes, I just want a load of dummy data in there to test the system I have out.
Is there a direct statement for this? Or is there a way to export all data except ID (or change ID to MAX(ID) + 1 or AUTO INCREMENT)?
You can try this:
INSERT INTO your_table_name(parent_id,priority,text,shortname,weighting,g_or_a,
dept,ksf,day_start,day_end,date_start,date_end,depends_on,is_question,budget,
ccode,responsible,accountable,consulted,informed)
(SELECT parent_id,priority,text,shortname,weighting,g_or_a,dept,ksf,
day_start,day_end,date_start,date_end,depends_on,is_question,budget,ccode,
responsible,accountable,consulted,informed FROM your_table_name);
Firstly, insert one row in the table 'your_table_name'. Replace your_table_name with the actual table name in above code & execute the code repeatedly until it satisfies the required row numbers. I think it should work.
Put 1 record and then run:
insert into mytable select * from mytable
10 times. This will give you 1024 records. Continue until satisfied.
You could use an INSERT and the values would be a SELECT, just don't select the primary key and don't define it in the insert fields.
Imagine a table with 3 fields, the_pk, field_1, field_2
Something like
INSERT INTO the_table(field_1, field_2) (SELECT field_1, field_2 FROM the_table)
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