Php and MySql - copy data from one to another table - php

I just want to know how if it is possible to copy data from one table column to another in the same database? The code below is not working with my version of MySQL but it should be working.
UPDATE `table1` SET `table1.column1` =
(SELECT `table2.column2` FROM table2 WHERE `table1.city` = 'table2.city')
WHERE `listing` ='5'
Do you have some idea ?
Thanks !
Guys, As I thought solution is possible with php script and solution for this problem is to make a short script which is gonna do all this up with while loop :)
Thanks everybody on joining this discussions !
The solution was in `` marks between them should be column names,
this works nice, enjoy !

Yes you can do with INSERT ... SELECT Syntax like:
INSERT INTO database2.table1 (field1,field3,field9)
SELECT table2.field3,table2.field1,table2.field4
FROM table2;
Check Mysql

It reads like you're attempting to perform an update, not an insert.
UPDATE table1 AS t1 INNER JOIN table2 AS t2 ON t1.city=t2.city
SET t1.column1 = t2.column2
WHERE t1.listing ='5'
In the case that you do need to copy data from one table to the other then you need to follow this syntax:
INSERT INTO tablename (field1,field2,field3) (SELECT field1,field2,field3 FROM another_table);

Try this.
UPDATE table1, (
SELECT table2.city, table2.column2
FROM table2 ) table2
SET table1.column1 = table2.column2
WHERE table1.city = table2.city
AND listing ='5'

Try with
INSERT INTO table2
AS (SELECT *
FROM table1
WHERE table1.city = table2.city AND
listing = 5);
as per column try like
INSERT INTO table2 (columnname1) values (SELECT columnname1 FROM table1
WHERE table1.city = table2.city AND
listing = 5);

Related

Using Multiple WHERE clause in an INSERT INTO ... SELECT statement

Am having two tables inside my Database lets say Table1 and Table2, and am trying to copy the values from a specific rows(fields) in Table2 to a specific single row on Table1, The specific rows to be moved are determined by the User Input of Particular UserID of each row from Table1.
Table Structure:
Table 1: a_uid,a_FName,a_Username,a_PhoneNo,b_uid,b_FName,b_Username,b_PhoneNo......
And Table 2 Structure:
Table 2: uid,FName,Username,PhoneNo.....
Am uing the INSERT INTO .. SELECT statement, but with multiple WHERE clause but its giving me erros
INSERT INTO table1 WHERE uid='userinput1' (b_FName,b_Username,b_PhoneNo,) SELECT FName,Username,PhoneNo FROM table2 WHERE uid='userinput2';
But am getting Error
This type of clause was previously parsed. (near WHERE)
If the row in table1 already exists, you need an UPDATE .. JOIN statement instead of INSERT .. SELECT.
UPDATE table1 t1
JOIN table2 t2 ON t2.uid='userinput2'
SET t1.b_FName = t2.FName,
t1.b_Username = t2.Username,
t1.b_PhoneNo = t2.PhoneNo
WHERE t1.uid='userinput1'
If you don't know if the row in table1 already exists, you can use an INSERT .. SELECT .. ON DUPLICATE KEY UPDATE statement:
INSERT INTO table1 (uid, b_FName, b_Username, b_PhoneNo)
SELECT 'userinput1', FName, Username, PhoneNo
FROM table2
WHERE uid = 'userinput2'
ON DUPLICATE KEY UPDATE
SET b_FName = VALUES(FName),
b_Username = VALUES(Username),
b_PhoneNo = VALUES(PhoneNo)
Note that uid sould be primary keys or at least unique in both tables.
For sql-server it would be something like this:
INSERT INTO table1 (uid,FName,Username,PhoneNo)
SELECT #userinput1, FName,Username,PhoneNo
FROM table2
WHERE table2.uid=#userinput2;

How to update value when moving row to another table

I have 2 tables:
Table1
Table2
When I move a row from table1 to table2, I also want to update the datetime field and 1 more field.
Say both table have identical column like this:
id
shipped_by
datetime
other_column
I have the following sql line, but it is not working of course. But I want to have it something like that.
$query = "INSERT INTO table2
SELECT * FROM table1
WHERE id = '$id' UPDATE table2
SET shipped_by='$shipped_by', datetime='$datetime'";
The variable $shipped_by selects the userid, and $datetime date from now.
Can anyone help me with this sql code to make it work? I cannot figure it out.
Thank you.
To insert data form table1 with some column data modified can be done with insert and select without update.. select * should be used here, each column must be listed except for modified ones..
$query = "INSERT INTO table2
SELECT id, '$shipped_by', '$datetime', other_column FROM table1
WHERE id = '$id'";

MySQL very simple join example requested with subtable

I keep falling back into questions with MySQL joining.
And I would like to request a very simple example I could use to continue my journey of understanding learning the MySQL syntax.
Let's say I got the following table's
test_testtable
testtable_id
testtable_name
testtable_user
testtable_option
testtable_textfield
test_testlink
testlink_id
testlink_link
testlink_address
test_address
address_id
address_name
address_phone
address_email
address_street
address_city
address_zip
I would like to make a selection like :
SELECT * (lets say I would define the fields) FROM `test_testable`
JOIN `test_testtable`.`testtable_id` = `test_testlink`.`testlink_link`
AND
JOIN `test_testlink`.`testlink_addres` = `test_address`.`address_id`
WHERE `user_id` = 5
Hence the linking structure is like:
test_testtable.testtable_id = leading
table test_testlink is a table to link the table test_testtable and test_address
And linking table test_testlink uses the field testlink_link to link to the table test_testtable, and uses the field testlink_address to link to the table test_address
This does not work. FOR ME.. Since I continuously seem to fail of catching the correct syntax logic.
So I hope that someone could give me a small example of how to correctly implement such a simple yet critical query!
TIAD!!
A general approach :
SELECT table1.* FROM table1
JOIN table2 ON table2.id_table1 = table1.id
JOIN table3 ON table3.id_table2 = table2.id
WHERE table1.id = 10
For your purpose :
SELECT * (lets say I would define the fields) FROM `test_testable`
JOIN `test_testlink` ON `test_testtable`.`testtable_id` = `test_testlink`.`testlink_link`
JOIN `test_address` ON `test_testlink`.`testlink_addres` = `test_address`.`address_id`
WHERE `user_id` = 5
Please read the reference
You are using wrong syntax. You should mention which tables to join first then based on which fields.
SELECT * (lets say I would define the fields) FROM `test_testable`
INNER JOIN test_testlink
ON `test_testtable`.`testtable_id` = `test_testlink`.`testlink_link`
INNER JOIN `test_address`
ON `test_testlink`.`testlink_addres` = `test_address`.`address_id`
AND `test_testtable`.`user_id` = 5
select * from testlink JOIN testtable ON testlink.tableid = testtable.ID
JOIN testaddress ON testlink.addressid = testaddress.ID
WHERE testtable.ID = 5

How can I select a row that may contain swapped fields?

How can I select one row from that table http://i.stack.imgur.com/27cu9.jpg where values of 'user_1' and 'user_2' may look like
user_1 user_2
1 2
2 1
In other words I want to select a field that contains 2 users with submitted=1 no matter in which field the value is.
Here is a simple query that does this:
select *
from t
where submitted = 1 and 2 in (user_1, user_2)
If I understood your question, I think you need to JOIN the table on itself if you are trying to return rows that have corresponding users (1,2) and (2,1):
select t1.*
from yourtable t1
join yourtable t2 on
t1.user_1 = t2.user_2 and t1.user_2 = t2.user_1
SQL Fiddle Demo
If however you are just trying to see if user 2 exists in either of the fields, then look at Gordon's post.
Use this:-
select * from tblname as t1, tblname as t2 where
t1.user_1 = t2.user_2 and t1.user_2 = t2.user_1 and t1.user_1<>t1.user_2
EDIT:-
Updated the query so that the rows with the same values do not appear in the result.

remove identical values from 2 mysqli queries

I want to make 2 mysqli queries. But if there are values in the second query that are Identical to the values yielded from the first query I want to exclude those values from the result set. What I have now only seems to work for 1 identical value. the rest of the identical values are shown. how should I change this? Thanks.
$query1 = $db->query("SELECT colTab1 FROM table1");
while ($result1 = $query1 ->fetch_assoc()) {
$query2 = $db->query("SELECT colTab2 FROM table2 WHERE colTab2 <> $result1[colTab1]");
echo $result1['colTab1']."<br>";
}
while ($result2 = $query2 ->fetch_assoc()) {
echo $result2['colTab2']."<br>";
}
}
Well, you can modify your second query as follows:
SELECT colTab2 FROM table2 WHERE colTab2 NOT IN (SELECT colTab1 FROM table1)
Or maybe you just want to select the UNION of the two tables (which will omit duplicates by default):
SELECT colTab1 FROM table1
UNION
SELECT colTab2 FROM table2
(Note that relying on UNION to omit duplicates between the two recordsets is not quite the same thing, as any duplicates that exist within each recordset will also be omitted; if that is a concern, one can SELECT DISTINCT ... UNION ALL SELECT DISTINCT ... instead).
Just run one query; why bring back data from the database to filter on? Filter in the DB.
SELECT colTab2
FROM table2
WHERE colTab2 NOT IN (
SELECT colTab1
FROM table1
)

Categories