How to move row to another row in mysql - mysql

I want to move one or two rows(with data) for e.g. from end to first position or from middle to end? With phpmyadmin with GUI there isn't option to moving rows.
Another question:
How to move one table to another table by copying data?

Rows position in a resultset are determined by ORDER BY clause or by "chance" if it's not specified, so moving from a position to another in absolute has no meaning.
You can use INSERT SELECT statement to copy data from table1 to table2 if they have the same structure.
INSERT INTO table2
SELECT *
FROM table1

You may copy data with help of a query:
INSERT INTO table2 (col1, col2, col3) SELECT col1, col2, col3 FROM table1

Related

Overwriting content from one table to existing table

I have two different tables with two colums that have the same name, datatype and size.
My issue:
When I try to copy the content from one column to the other,
update Table1
set Column4 = (select Column1 from Table2);
I get an error. (subquery returns more than one row)
My question:
Is there a way for me to copy the content from Table2 to Table1 in a similar fashion as the code shows above?
Use Insert ... Select
Insert INTO Table1 (Column4)
SELECT Column1 FROM Table2

Copy row from one MySQL DB to another

How it's possible to copy one entry row of a table with same data to another (same ID, same data values) Database -> same table?
Example:
Table Units:
UID Department Name Item
67 HR John Doe Table
If both tables equal no. of columns and in same order you want to insert then just use below simple query-
INSERT INTO mytable SELECT * FROM units WHERE uid=67;
If you want to insert selected column in another table's selected columns and in your order then use below-
INSERT INTO mytable(col1,col2,col3,col4) SELECT uid,department,`name`,item FROM units WHERE uid=67;
If I understand you correctly you want to copy some rows to table of another DB.
Try INSERT SELECT Query:
insert into db1.tbl(id,col1,col2)
select id,col1,col2 from db2.tbl;
Use trigger option in mysql to make new table with same data.
Suppose if you want to copy table1 data to table2 with some condition.
INSERT INTO table2 (ID, NAME) SELECT Col1, Col2 FROM table1 WHERE Col1='<Your_Condition>';
Here table2 have fields like ID and NAME and table1 have fields like Col1 and Col2.
In that case, above query copy table1 data to table2 on these fields where condition matched on table1, if you want to copy whole data of table1 then remove the WHERE condition from Select Query.

Inserting data from one table to another without duplication in access-VBA

I want to insert data from a table WorkTableA to another table TableA, without duplicating the data (i.e. do not insert into WorkTableA if the customer name already exists).
Is there a way of doing it through VBA code.
The field name and their properties in both tables are identical.
What you need is an INSERT INTO statement
INSERT INTO WorkTableA
( CustomerName, Col2, Col3...)
SELECT CustomerName, Col2, Col3
FROM TableA LEFT JOIN WaorkTableA ON TableA.CustomerName = WORKTABLEA.CustomerName
WHERE WorkTableA.CustomerName IS NULL
Something like this might work.
The SELECT part of the statement will select only the ones that DO NOT EXIST in WorkTableA

Copy a table to another table with different structure

I have 2 tables in the DB:
Table1
Table1_Temp
The Table1_Temp was generated from a CSV. it is almost identical to Table1 but different because all Table1_Temp fields are VARCHAR and it has some irrelevant fields.
I need to move the Data from Table1_Temp to Table1 but to keep the structure of Table1, and disregard the unnecessary fields from Table1_Temp.
How can I do it?
Choose the columns to use and cast them to the necessary type in your select
insert into table1 (col1, col2, col3)
select cast(col1 as signed), col5, col7
from Table1_Temp
if both tables are on a different database (and different column)
INSERT INTO db1.table1 (Acol1, Acol2, Acol3)
SELECT Bcol1 AS Acol1, Bcol2 AS Acol2, Bcol3 AS Acol3
FROM db2.table1_temp
This will only work if both databases are under 1 server (in this instance "localhost")

INSERT result of a SELECT and other values as well

I want to know if this is possible without a procedure or server side calls into the database.
I am trying to insert values into a table based on a select, and other values that will be provided from the server.
The select statement will return more than one result.
I am aware of the existence of INSERT SELECT, but is there any SELECT INSERT ? or a way to insert based on the results of a select ?
thank you
Not really sure what seems to be the problem.
You can do like this:
INSERT INTO table (columns)
SELECT
column or column expression1,
column or column expression2,
…
constant or constant expression1,
constant or constant expression2,
…
FROM a set of tables/joins
WHERE …
Not necessarily in that order (columns, then constants), no. You can mix columns with constants any way you like, just follow the order of the columns you are inserting into.
Was that what you were asking about?
I don't see why an
INSERT INTO yourtable(col1, col2, col3)
SELECT col1, col2, col3
FROM yourothertable
doesn't work for you. But you could always do a SELECT INTO #temptable to save your query in a temporary table and then you could INSERT that data or manipulate it prior to inserting. This is just a long way around the original idea, though.
Am I misunderstanding your questions?
Yes. Use this query:
INSERT INTO FOO (oof, rab) SELECT (foo, bar) FROM BAR;
I think you can do this:
INSERT INTO targetTable (col1, col2, col3)
SELECT col1, col2, col3 FROM sourceTable
UNION ALL
SELECT 'something' AS col1, 'something else' AS col2, 'yet something else' AS col3 FROM DUAL;