how can I create a new table selecting specific rows of the old table ?
The selected 3 rows have field1 = 243, 245 and 248 respectively.
Also I need to select the rows with field1 > 0 from table3.
thanks
You can use CREATE TABLE...AS SELECT... to make a table defined by the columns and data types of a query result set:
CREATE TABLE NewTable AS
SELECT * FROM Table3
WHERE field1 IN (243,245,248);
I can't tell what you mean about field1>0. I'll leave that to you.
create table new_table like old_table;
insert into new_table select * from old_table where field1 in (243,245,248);
insert into new_table select * from table3 where field1 >0;
Related
For example, I have a table like this:
id
|02|
|01|
|03|
|05|
|04|
I need to alter the table with 05 at the top:
id
|05|
|01|
|02|
|03|
|04|
SELECT * FROM table1 ORDER BY id=05 desc,id works. But I want to alter the table.
ALTER TABLE table1 ORDER BY id can re-order the table data. But
ALTER TABLE table1 ORDER BY id=05 desc,id does not work.
I have an application which the old version does not order the table, so it displays '02' on top. I cannot update the application any time soon, so I have to re-order the table instead.
ALTER table isn't used to manipulate data, it's used to modify table structure. Your best bet would be to SELECT into a temp table, TRUNCATE, then re-insert from the temp table.
Something like:
SELECT *
INTO #temp
FROM table1
ORDER BY id=05 desc,id;
TRUNCATE table1;
INSERT INTO table1
SELECT * FROM #temp;
DROP #temp;
I can't find a way to do this without changing the structure of the table. UPDATE works with this kind of ordering, so I have to insert a new column, update it with a variable and alter it
ALTER TABLE table1 ADD COLUMN id2 INT NULL;
SET #i=1;
UPDATE table1 SET id2=(#i:=#i+1) ORDER BY id=05 desc,id
ALTER TABLE table1 ORDER BY id2;
Given the following table structure:
ID
Field1
Field2
Field3
I would like to duplicate a record in the table. The pseudo code would look something like this:
DUPLICATE * IN MyTable WHERE ID = 3
Here are my constraints:
I don't want the ID duplicated.
I do not now how many fields or their types are in the table.
I do not want to do this in PHP.
You can use a select statement to insert rows. If you want to insert a new row with all of the same values, just write a select statement to get the values you want. Try something like this:
INSERT INTO myTable (field1, field2, field3)
SELECT field1, field2, field3 FROM myTable WHERE id = 3;
Here is an SQL Fiddle example.
EDIT
If you don't know how many fields, an option you have is to use a temporary table. See this article for example. The reason you need a temporary table, is because you cannot simply re-insert the row, because you will have a duplicate key error. So the logic will be as follows: Add row to new table, change the id, insert duplicated row with new id into old table, and drop the temporary table.
Assuming your id column is auto_increment, you can set the new id to the current maximum id + 1. You don't have to do that, but as long as you have some way of updating the id to something that does not already exist, this will work. Try this instead:
CREATE TEMPORARY TABLE temp SELECT * FROM myTable WHERE id = 3;
UPDATE temp SET id = (SELECT MAX(id) FROM myTable) + 1;
INSERT INTO myTable SELECT * FROM temp;
DROP TABLE temp;
Here is another SQL Fiddle example.
I have two tables with same elements, the only differnce is there ids' which are primary key and auto increment.
Table1 | Table2
id1(PK)| id2(PK)
col1 | col1
col2 | col2
col3 | col3
I know some quick ways to do that like,
INSERT INTO table2 SELECT * FROM table1 where id1 = 2
while using such method the content of table2 has id2 = 2 as it copies all the fields directly to table2 from table1, to restric that,
I can also use a method
INSERT INTO table2(col1,col2,col3) SELECT col1,col2,col3 FROM table1 WHERE id1 = 2
such way is good for short tables, but I have lot of columns in my table.
I need a quick way to copy all the columns from table1 to table2 leaving the primary columns which is id2, as it is autoincremented.
Its like I want to copy a specified row from table1 to table2 with different id2(which will be generated as its autoincremented).
Are there any possibilities.
If you do not want to mention column names but want to copy all, then try copy all data into a temp table, then drop pk_id field, and then copy rest of the fields into desired table, lastly drop the temp table.
Refer to one of my answers to a similar queries:
Mysql: Copy row but with new id
We can use temporary table to buffer first from main table and use it to copy to main table again. We can drop the pk field from the temp table and copy all other to the second table.
With reference to the answer by Tim Ruehsen in a referred posting:
CREATE TEMPORARY TABLE tmp_table SELECT * from first_table WHERE ...;
ALTER TABLE tmp_table drop pk_id; # drop autoincrement field
# UPDATE tmp_table SET ...; # just needed to change other unique keys
INSERT INTO second_table SELECT 0, tmp_table.* FROM tmp_table;
DROP TABLE tmp_table;
Copy all rows from table1 except that have same id in table2
INSERT INTO table2(col1, col2, col3)
SELECT t1.col1, t1.col2, t1.col3
FROM table1 t1
WHERE t1.id1 not in (SELECT id2 FROM table2);
I have a MySql table which has about 100k rows. there is one field say id which contains numbers from 1-35. all these records fall in this range of id i.e. all these records have value of id column between 1-35.
Now i want to create another table which will have one row of each id. i.e the new table should have 35 rows only.
How to go about it ?
create table new_table (id int);
insert into new_table
select distinct id from big_table;
Edit:
You can create the new_table by outputting the big_table create script and changing the name.
SHOW CREATE TABLE big_table;
/* modify the name of the output and execute */
insert into new_table
select * from big_table group by id
You have a table with 100.000 rows, and you want a new table with 35 rows. What values do you want for the remaining columns?
If the answer is: doesn't matter, this works:
CREATE TABLE newTable
SELECT * FROM yourTable
GROUP BY ID;
If you only want the IDs,
CREATE TABLE newTable
SELECT DISTINCT ID FROM yourTable;
You can copy data from one table to another even difference database(Schema) as following
INSERT INTO [DestDatabase].[DestTablName]
SELECT [ColumnName] FROM [SourceDatabase].[SourceTablName];
So, you can use two way:
1:
INSERT INTO tbl_New
SELECT DISTINCT id from tbl_Original;
2:
INSERT INTO tbl_New
SELECT id from tbl_Original GROUP BY id;
Sometimes if I want to quickly copy records from one table to another (that has the same structure) I use a query like this:
INSERT INTO table2 SELECT * FROM
table1 WHERE id = SOME_VALUE
How can I add a ON DUPLICATE KEY UPDATE to this statement? I tried this:
INSERT INTO SELECT * FROM table1 WHERE
id = 1 ON DUPLICATE KEY UPDATE SELECT
* FROM table1 WHERE id = 1
But I get an error. Is there away to accomplish the query above with out individually listing each column in the query?
P.S. Yes, I realize that it is not good practice to have multiple tables with identical structures, but sometimes you just don't get control over everything in the workplace!
The below UPDATES if there is no PK duplication and INSERTs is there is:
REPLACE INTO table2(field1, field2, field3)
SELECT field1, field2,field3 FROM table1
WHERE id=1;
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html Just use the SELECT field_name from the other table like in dnagirls example