Jump to content

Recommended Posts

hi,

 

i need help, is there any code available

that can copy specific items in a table to another table and

verifies if there's any duplicates also?

 

example

table 1

 

id | name | address | email | contact | company

 

in table 2

id | name | address | email

 

how do i copy field id, name, address and email in table 1 going to table 2

and omits items that are already existing in table 2 via id and name?

 

hope you could help me with this.

 

 

thanks so much!

Link to comment
https://forums.phpfreaks.com/topic/61317-need-help-on-queries/
Share on other sites

yes its called a dual/multi level query

<?php
$q = "select * from `table` WHERE (select From `table2` WHERE)";
?>

I'm not exactly sure on its syntax I know it can be done similar to this method

I think its this, (not sure)

<?php
$q = "SELECT * From `table1` WHERE Name != 'SELECT Name From `table2`' && Address != 'Select address From `Table2`'";
?>

I think...

Link to comment
https://forums.phpfreaks.com/topic/61317-need-help-on-queries/#findComment-305116
Share on other sites

Try it like this:

I am not sure is this u want or not but it will copy the data from one table to another.

 

$sql = "INSERT INTO table1 (name,phone,address,date)
            select name,phone,address,date 
            from table2 where id = '001' ;
            ";

 

OR from php point of view:

 

$sql = "INSERT INTO table1 (name,phone,address,date)
            select name,address,'$phone','$date' 
            from table2 where id = '$id' ;
            ";

 

Hope it will help.

Link to comment
https://forums.phpfreaks.com/topic/61317-need-help-on-queries/#findComment-305132
Share on other sites

hi,

 

i apologize if my question is not clear.

currently i will be running a php page in cron

that will automatically update

table 2

with whatever new item is in table 1

 

and functions that i might needs is a query or php code that will

only copy new items from table 1 that is not yet existing

in table 2

 

table 1 has this fields

id | name | address| phone | company

 

and i only need to get new items in table 1 going to table 2

for this field

id | name | address

wherein in it will validate this 3 fields if the 3 fields

are the same, it will not copy it anymore.

 

hope you could help me with this.

 

thanks!

Link to comment
https://forums.phpfreaks.com/topic/61317-need-help-on-queries/#findComment-305137
Share on other sites

I've never heard of a copy function for mysql (as a function)

You need to query it out with * than say

<?php
while($row = mysql_fetch_array($result){
//insert each match
}
?>

 

this is exactly how i would do it, the while loop will cycle through an array.  to refer to individual elements, use

$row['column_name']

 

to add and check you will need an embedded WHILE loop.  so execute SELECT * queries on both tables

<?php

$qry1 = "SELECT * FROM table1";
$qry2 = "SELECT * FROM table2";
$query_from_table1 = mysql_query($qry1);
$query_from_table2 = mysql_query($qry2);

while($row1 = mysql_fetch_array($query_from_table1)){
    while($row2 = mysql_fetch_array($query_from_table2)){
         if (($row1['column_name1'] != $row2['column_name1']) AND ($row1['column_name2'] != $row2['column_name2'])){  // Perform for all columns
              // add data cause the data is new
         } else {
              // report that there is a match in the data
         }
    }
}

dont copy and paste this, use your respective column names and table names where indicated.  hope this helps :)

 

 

also, i believe there is a function in MySQL that will allow you to merge tables.  if you think about it, copying elements in one to another without overwriting is merely merging data sets.

Link to comment
https://forums.phpfreaks.com/topic/61317-need-help-on-queries/#findComment-305142
Share on other sites

hi i just want to ask,

 

since i wanted to copy items in table 1 to table 2

which doesnt have duplicates.

 

will this work?

INSERT * FROM table1 LEFT JOIN table2 WHERE table2.id IS NULL

 

i haven't tried it, but i just wanted to check first if it will work

 

thanks!

Link to comment
https://forums.phpfreaks.com/topic/61317-need-help-on-queries/#findComment-305183
Share on other sites

heres more info on JOIN

 

http://dev.mysql.com/doc/refman/4.1/en/join.html

 

im not familiar with the function myself.  its probably not the fastest way to do what you need, but i would use the nested WHILE loop. :)

 

if this needs to be done repeatedly, you will want to use PHP for it so you can call it again and again.  but tbh, if this only needs to be done once, you might consider using MySQL queries into a terminal (if you have access) or via a MySQL GUI based editor, such as phpMyAdmin.

Link to comment
https://forums.phpfreaks.com/topic/61317-need-help-on-queries/#findComment-305352
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.