copy one table to second table with current date - php

hy i am using Mysql (phpmyadmin) with php and i have two tables table1(id,name) and table2(id,date,name)
now i want to copy all rows of table1 to table2 and in table2 in date column should store current date of system, for this am trying following query which is giving error
'INSERT INTO table2 (id, Date, name)SELECT id,'.date("Y-m-d",mktime()).'nae FROM table1 WHERE id=2'
can you correct my query ? i tried different ways but not working any method. there is no error in connection with database all are ok, i tested connection its fine

How about this:
'INSERT INTO table2 (id, Date, name) SELECT id,"'.date("Y-m-d",mktime()).'",name FROM table1 WHERE id=2'

Related

MYSQL Insert with Select and if select returns more than 1 value insert both

Quite simple but I dont get it. Any Solution?
For example: if there is 2 times the name peter. But you should insert both.
insert into table1 (id,firstname,zip) values ('%s',(select nametable from table2 where name='peter'),'1234');
I hope its understandable
Edit: you get the id.. i just changed it...
I want to insert something where I have to select from another table. But the problem is sometimes i get more than 1 result. If there is more than 1 result I want to insert all results.
insert into table1 (id, name, zip) ...
And what would you like to insert into it?
... select id, nametable, '1234'
from table2
where name='peter'
Now you connect those 2 pieces and it should work

PHP MySql Insert columns form one table to another

I want to insert my table data from copying another table with some new given data.
I use this query
$sql = "INSERT INTO table2(name, city, email,money)
hasib,SELECT table1.city, table1.email,
newsletter_subscribers.email
FROM table1 WHERE name='jesy',100";
But its not Work
You must give all columns inside the select statement columns, even constant values, e.g.
insert into table2(name, city, email, money)
select 'hasib', city, email, 100
from table1
where name = 'jesy'
If you want to take values from multiple tables, as your select statement suggests, you must look into joins.

MYSQL / PHP - merge two tables with different columns

I want to access a database merging two tables from a php script in order to output the result to an html table. Both tables are pretty much the same except one column I need only exists in one table. I don't have direct access to the database.
INSERT INTO globalusers
(surname, firstname, email, location)
INSERT INTO localusers
(surname, firstname, email)
Location exists only in globalusers. I need to use this column as a dropdown filter in my table and add the column location with the string "local" to the data coming from the localusers table.
Here's a fiddle
So the expected result would be one table with four columns and the string "local" in the location column for the localusers table.
What is the select that I need?
MySQL version: 5.5.36
Sounds like you're looking to combine the results of the two tables. If so, you can use UNION ALL for this:
SELECT surname, firstname, email, location
FROM globalusers
UNION ALL
SELECT surname, firstname, email, 'local'
FROM localusers
Updated SQL Fiddle

How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

I have two tables as it is seen in the schematic picture. I want to copy data from three columns URI, fields and details in table1 and insert them into table2 if date in table1 is greater than 12/11/2013. Something like below query:
INSERT INTO table2 (all_links, fields_one, fields_two)
FROM table1 (URI, fields, details) WHERE date>"12-11-2013 00-00-00";
Could you please help to solve this problem?
Do it like
INSERT INTO table2 (all_links, fields_one, fields_two)
select URI, fields, details FROM table1
WHERE date > "12-11-2013 00-00-00";
In case tables are in different DB (assuming dbo is default owner)
INSERT INTO DB1.dbo.table2 (all_links, fields_one, fields_two)
select URI, fields, details FROM DB2.dbo.table1
WHERE date > "12-11-2013 00-00-00";
Try something like this:
INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
And check the documentation:
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
INSERT INTO table2 (all_links, fields_one, fields_two) select URI, fields, details from table1 where date>"12-11-2013 00-00-00";

Is it possible to insert values from one table into another AND update field values in same query?

I have two tables. One table is meant to serve as a transaction history and the other is a log of member details. When a report is run, I want to move pieces of the member details into the transaction history but ALSO update some field records which would not otherwise exist.
Is it possible to select all records which meet a specific criteria, insert only pieces of the matching row into another table AND update other fields in a single query?
For example:
In table 2, i have member name, date registered, and memberid. I want to move the above records into table 1 but also update the field (status) equal to 'processed'.
Note: I am also using php and pdo to connect to a mysql database.
Is this possible within a single query?
You didn't specify whether the rows you want to update are the same as the ones you are inserting. I am assuming they are:
insert into table1
(member_name, date_registered, memberid, status)
select member_name, date_registered, memberid, 'processed'
from table2
where SomeField = MyCriteria
After much consideration - I decided to use ircmaxell's advice and simply run multiple queries. It ends up not only making things easier but allows me to customize my sorting much easier.
As he said above, "Don't get caught in the trap of less is always better"
Yes:
SELECT *, "processed" INTO table2 FROM table1
You will have to adapt based on the table structures, perhaps even write out all the fields:
SELECT field1, field2, field3, "processed" INTO table2 FROM table1
Of note, this assumes you want to write into table 2 including the processed variable (Might I suggest a boolean?) if you want the "Processed" in the other table it will get more complicated.
Edit: Apparently mysql doesn't support select into so...
INSERT INTO table2 SELECT field1, field2, field3, "processed" FROM table1
Redfilters code works

Categories