Gossamer Forum
Home : General : Perl Programming :

Need help with an SQL statement...

Quote Reply
Need help with an SQL statement...
Hey everyone. My abilities to write enhanced SQL statements are very lacking and was wondering if I could get some assistance. I will explain what I have, then ask my question...

I am using DBManSQL and use it for a Marine Corps guestbook. Currently there are 45,700 records in the guestbook. There are 4 tables in the guestbook db, 2 of which are guestbook and guestbook_users. These 2 tables are linked by the username field and should be a 1:1 ratio with the amount of records in each table (one user record for one guestbook record). There are 53,000 records in the guestbook_users table...7,300 more than in the guestbook. This is due to user records that aren't linked to guestbook records because of bad email addresses when the user signed up for an account!

I know how to write a statement to show me each user record that has a corresponding guestbook record:
SELECT guestbook_users.username, guestbook.userid FROM guestbook_users JOIN guestbook WHERE guestbook_users.username = guestbook.userid

What I need to know is an SQL statement that will compare the two tables and show me records from each table that DON'T have a matching record in the other table so I can delete these records! Any help is greatly appreciated!

AJ
Webmaster, TheFew.com
http://www.thefew.com
Semper Fi, Do or Die!
Quote Reply
Re: Need help with an SQL statement... In reply to
The delete statement does not allow joining two or more tables. You should select all records that not have correspond record in the other table and delete. You need some code to do this.

Mark

Quote Reply
Re: Need help with an SQL statement... In reply to
Mark...thanks for the quick reply. I'm not trying to delete the records with the SQL statement. I want to know what 7300 records don't have the corresponding record in the other table. Once I know that, I can dump the usernames to a file and create a query to delete them all! I can accomplish this if I knew the SQL statement to run the query to show me these "bad user" records!

AJ
Webmaster, TheFew.com
http://www.thefew.com
Semper Fi, Do or Die!
Quote Reply
Re: Need help with an SQL statement... In reply to
Could you give the structure of two table and I will write some code to list the user has not any record in othe table.

Mark

Quote Reply
Re: Need help with an SQL statement... In reply to
Here are snapshots of the table properties...

http://www.thefew.com/temp/gb.jpg
http://www.thefew.com/temp/gbusers.jpg


AJ
Webmaster, TheFew.com
http://www.thefew.com
Semper Fi, Do or Die!
Quote Reply
Re: Need help with an SQL statement... In reply to
Use this script with some change(provide databsename,username,password to connect to Mysql) and your key to link from guestbook_users table to guestbook.

#!/usr/bin/perl
use DBI;
use CGI;
$|++;

# Run the program and trap fatal errors.
# ----------------------------------------------------------------
eval { &main; };
$@ and die("Fatal Error: $@");

sub main {
# ================================================================
# We are either displaying the add screen, or processing a command.
#
@dbi_connect = ('DBI:mysql:yourdatabsename:localhost', 'yourusername', 'password');
$dbh = DBI->connect (@dbi_connect) or die("Can't connect to database. Reason: $DBI::errstr");
my($username,$userid,$numrows,$listuser);
$qry=qq~
select userid,username from guestbook_users

~;
$sth=$dbh->prepare($qry) || die("Can't prepare query.Reason:$DBI::errstr. Query: $qry");
$rs=$sth->execute() || die("Can't execute query.Reason:$DBI::errstr. Query: $qry");
if ($sth->rows){
for($i=0;$i< $sth->rows;$i++){
($username,$userid) = $sth->fetchrow_array;
$query = qq~
select count(*) from guestbook where userid = $userid
~;
$sth1=$dbh->prepare($query) || die("Can't prepare query.Reason:$DBI::errstr. Query: $query");
$rs1=$sth1->execute() || die("Can't execute query.Reason:$DBI::errstr. Query: $query");
if ($sth1->rows){
$numrows = $sth1->fetchrow_array;
if(!$numrows){
$listuser .="$username
";
}
}
}
}
print "Content-type: text/html\n\n";
print "list users has not any record in guestbook
";
print $listuser;
$dbh->disconnect;
}


Quote Reply
Re: Need help with an SQL statement... In reply to
atlas...thank you very much! There were 2 problems...you forgot to incude the $DBH->quote so it would put '' around the username. The other problem was not a problem of your script, but of the web browser time-out. It would not make it through the checking of all 45,700 records within the time IE or Netscape times out! I had to run it from a telnet session (perl5 extrausers.pl). I did customize the script a bit as well. I created an empty file called badusers.txt and FTPd it to the directory where extrausers.pl is. I then changed the script to open the txt file and write the non-matching usernames to the file. After looking throught the txt file and doing a few verifications, I changed the script from writing the non-matching usernames to the txt file, to actually creating a DELETE query and deleting it. The following script contains both writing to txt file and doing the delete query. You put # in front of the one you don't want to do.

Code:
#!/usr/bin/perl
use DBI;
use CGI;
$|++;

# Run the program and trap fatal errors.
# ----------------------------------------------------------------
eval { &main; };
$@ and die("Fatal Error: $@");

sub main {
# ================================================================
# We are either displaying the add screen, or processing a command.
#
@dbi_connect = ('DBI:mysql:guestbook:www.thefew.com:3306', 'guestbook', 'nemo6865');
$DBH = DBI->connect (@dbi_connect) or die("Can't connect to database. Reason: $DBI::errstr");
my($username,$userid,$numrows,$listuser);
# open (BADLIST, ">badusers.txt");
# flock(BADLIST, 2);

$qry=qq!
select username from guestbook_users
!;
$sth=$DBH->prepare($qry) || die("Can't prepare query.Reason:$DBI::errstr. Query: $qry");
$rs=$sth->execute() || die("Can't execute query.Reason:$DBI::errstr. Query: $qry");
if ($sth->rows) {
for($i=0;$i< $sth->rows;$i++) {
($username) = $sth->fetchrow_array;
$username_q = $DBH->quote($username);
$query = qq~
select count(*) from guestbook where userid = $username_q
~;
$sth1=$DBH->prepare($query) || die("Can't prepare query.Reason:$DBI::errstr. Query: $query");
$rs1=$sth1->execute() || die("Can't execute query.Reason:$DBI::errstr. Query: $query");
if ($sth1->rows) {
$numrows = $sth1->fetchrow_array;
if(!$numrows) {
$query = qq~
DELETE FROM guestbook_users WHERE username = $username_q
~;
$sth2=$DBH->prepare($query) || die("Can't prepare query.Reason:$DBI::errstr. Query: $query");
$rs2=$sth2->execute() || die("Can't execute query.Reason:$DBI::errstr. Query: $query");
# print BADLIST "DELETE FROM guestbook_users WHERE username = $username_q\n";
}
}
}
}
# close (BADLIST);
$DBH->disconnect;
}
AJ
Webmaster, TheFew.com
http://www.thefew.com
Semper Fi, Do or Die!