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?
Oct 25, 2002, 3:22 PM
Enthusiast (854 posts)
Oct 25, 2002, 3:22 PM
Post #2 of 8
Views: 7962
I think this is close but I'm a bit fuzzy on SQL syntax (no pun intended
)
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.

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
Oct 25, 2002, 3:58 PM
New User (4 posts)
Oct 25, 2002, 3:58 PM
Post #3 of 8
Views: 7951
this is my table structure:
# 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
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
Oct 25, 2002, 4:25 PM
Enthusiast (854 posts)
Oct 25, 2002, 4:25 PM
Post #4 of 8
Views: 7948
assuming you're using Perl, try this:
$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.
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.
Oct 25, 2002, 4:38 PM
Enthusiast (854 posts)
Oct 25, 2002, 4:38 PM
Post #6 of 8
Views: 7958
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.
Philip
------------------
Limecat is not pleased.
Oct 25, 2002, 6:53 PM
Enthusiast (854 posts)
Oct 25, 2002, 6:53 PM
Post #8 of 8
Views: 7897
no promises, since I don't have mySQL installed on my system for testing.
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.
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.