Gossamer Forum
Home : General : Databases and SQL :

matching two columns

Quote Reply
matching two columns
i have two tables. in both tables, there is a column that matches. i want to run a sql query that finds the matching image names, and inserts the corresponding description from table one into table 2. any clues?
Quote Reply
Re: [Deepu Sudhakar] matching two columns In reply to
I think this is close but I'm a bit fuzzy on SQL syntax (no pun intended Angelic)

update table2 set table2.description = table1.description where table1.imagename = table2.imagename

[update]hm... seems you can't do join on a update according to what I read on google groups.

Philip
------------------
Limecat is not pleased.

Last edited by:

fuzzy thoughts: Oct 25, 2002, 3:41 PM
Quote Reply
Re: [fuzzy thoughts] matching two columns In reply to
this is my table structure:
Code:
#
# Table structure for table `4images_images`
#

CREATE TABLE 4images_images (
image_id mediumint(8) NOT NULL auto_increment,
cat_id mediumint(8) NOT NULL default '0',
user_id mediumint(8) NOT NULL default '0',
image_name varchar(255) NOT NULL default '',
image_description text NOT NULL,
image_keywords text NOT NULL,
image_date int(11) unsigned NOT NULL default '0',
image_active tinyint(1) NOT NULL default '1',
image_media_file varchar(255) NOT NULL default '',
image_thumb_file varchar(255) NOT NULL default '',
image_download_url varchar(255) NOT NULL default '',
image_allow_comments tinyint(1) NOT NULL default '1',
image_comments smallint(6) NOT NULL default '0',
image_downloads smallint(6) NOT NULL default '0',
image_votes smallint(6) NOT NULL default '0',
image_rating decimal(4,2) NOT NULL default '0.00',
image_hits mediumint(8) NOT NULL default '0',
PRIMARY KEY (image_id),
KEY cat_id (cat_id),
KEY user_id (user_id),
KEY image_date (image_date),
KEY image_active (image_active)
) TYPE=MyISAM;
# --------------------------------------------------------

#
# Table structure for table `descr`
#

CREATE TABLE descr (
name varchar(255) NOT NULL default '',
descr text NOT NULL,
seclevel int(11) NOT NULL default '0',
PRIMARY KEY (name)
) TYPE=MyISAM;

this is the query i ran:
update 4images_images set 4images.image_name = descr.descr where descr.name = 4images_images.image_media_file

this is the result:
Unknown table 'descr' in where clause
Quote Reply
Re: [Deepu Sudhakar] matching two columns In reply to
assuming you're using Perl, try this:
Code:
my $sth = $dbh->prepare("select table1.* from table1, table2 where table1.imagename=table2.imagename");
$sth->execute();
while(my ($name, $desc) = $sth->fetchrow_array()) {
my $update = $dbh->do("update table2 set description=$desc where imagename=$name");
}
$sth->finish();

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy thoughts] matching two columns In reply to
sorry...but i have no clue how to use perl. php...yes
Quote Reply
Re: [Deepu Sudhakar] matching two columns In reply to
The idea will be the same, you'll just have to replace the Perl functions with the PHP functions. here's a link to the PHP MySQL reference.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy thoughts] matching two columns In reply to
In Reply To:
The idea will be the same, you'll just have to replace the Perl functions with the PHP functions. here's a link to the PHP MySQL reference.
sorry to bother you once again...im not very proficient with php. i can run scripts but im not too involved with writing them. i tried the perl alternative. i can run a script so i cant run them. if you detail the perl script, i can probably run it.
Quote Reply
Re: [Deepu Sudhakar] matching two columns In reply to
no promises, since I don't have mySQL installed on my system for testing.

Code:
#!/usr/local/bin/perl

use DBI;

my $database = "database";
my $username = "username";
my $password = "password";

my $changed = 0;

my $dbh = DBI->connect("DBD:mysql:$database", $username, $password) or die $!;

my $sth = $dbh->prepare("select table1.* from table1, table2 where table1.imagename=table2.imagename") or die $dbh->errstr;

$sth->execute() or die $sth->errstr;

while(my ($name, $desc) = $sth->fetchrow_array()) {
$dbh->do("update table2 set description=$desc where imagename=$name") or die $dbh->errstr;
$changed++;
}

$sth->finish();

$dbh->disconnect();

print "Content-type: text/plain\n\n";
print "$changed records updated successfully.";

Philip
------------------
Limecat is not pleased.