bnownlater
Programmer
I am having problems with my database and need someone to take a look at it for me. Here are my table setups:
PUB
---------
PUB_ID
PUB_NAME
PUB_DATE
ALT_PUB_DATE
POST_DATE
TITLE
ABSTRACT
PAGE URL
COMPANY
----------
ID
PUB_ID
CO_NAME
METATAG
-----------
ID
PUB_ID
TOPIC
STATE
-----------
ID
PUB_ID
THESTATE
Here is my SQL statement:
$whereand="";
If (isset($topic) && trim($topic)!="All Topics"{
$whereand=$whereand." AND TOPIC='".addslashes($topic)."'";
}
If (isset($company) && trim($company)!="All Companies"{
$whereand=$whereand." AND CO_NAME='".addslashes($company)."'";
}
If (isset($state) && trim($state)!="All States"{
$whereand=$whereand." AND THESTATE='".addslashes($state)."'";
}
If ($whereand!=""{
$whereand=substr($whereand, 4);
$where="WHERE $whereand GROUP BY PUB.PUB_ID";
}else{
$where="";
}
$SqlStr="SELECT PUB.*,METATAG.*,STATE.*,COMPANY.* FROM PUB
LEFT JOIN METATAG
ON PUB.PUB_ID = METATAG.PUB_ID
LEFT JOIN STATE
ON PUB.PUB_ID = STATE.PUB_ID
LEFT JOIN COMPANY
ON PUB.PUB_ID = COMPANY.PUB_ID ".$where;
echo $SqlStr;
$result = @mysql_query($SqlStr, $connection) or die("Couldn't execute select query"
If I don't use GROUP BY PUB.PUB_ID I get multiples of the same record? Not sure why. Also some records would not have a company or state associated with the record. So I guess I have to use a LEFT JOIN?? Another problem I am having is that I try to show all of the companies or states by using a SQl statement like:
//Select all company names from the COMPANY table to be displayed for a particular pub_id
$SqlStr="SELECT * FROM COMPANY WHERE PUB_ID=".$row['PUB_ID'];
//echo $SqlStr;
$result1 = mysql_query($SqlStr, $connection) or die("Couldn't execute select query"
The problem comes into to play when there is not a company (or state). The $row['PUB.PUB_ID'] has no value, but there is a record in the PUB table??
Really need help with the FOUR table SQL. Thanks.
PUB
---------
PUB_ID
PUB_NAME
PUB_DATE
ALT_PUB_DATE
POST_DATE
TITLE
ABSTRACT
PAGE URL
COMPANY
----------
ID
PUB_ID
CO_NAME
METATAG
-----------
ID
PUB_ID
TOPIC
STATE
-----------
ID
PUB_ID
THESTATE
Here is my SQL statement:
$whereand="";
If (isset($topic) && trim($topic)!="All Topics"{
$whereand=$whereand." AND TOPIC='".addslashes($topic)."'";
}
If (isset($company) && trim($company)!="All Companies"{
$whereand=$whereand." AND CO_NAME='".addslashes($company)."'";
}
If (isset($state) && trim($state)!="All States"{
$whereand=$whereand." AND THESTATE='".addslashes($state)."'";
}
If ($whereand!=""{
$whereand=substr($whereand, 4);
$where="WHERE $whereand GROUP BY PUB.PUB_ID";
}else{
$where="";
}
$SqlStr="SELECT PUB.*,METATAG.*,STATE.*,COMPANY.* FROM PUB
LEFT JOIN METATAG
ON PUB.PUB_ID = METATAG.PUB_ID
LEFT JOIN STATE
ON PUB.PUB_ID = STATE.PUB_ID
LEFT JOIN COMPANY
ON PUB.PUB_ID = COMPANY.PUB_ID ".$where;
echo $SqlStr;
$result = @mysql_query($SqlStr, $connection) or die("Couldn't execute select query"
If I don't use GROUP BY PUB.PUB_ID I get multiples of the same record? Not sure why. Also some records would not have a company or state associated with the record. So I guess I have to use a LEFT JOIN?? Another problem I am having is that I try to show all of the companies or states by using a SQl statement like:
//Select all company names from the COMPANY table to be displayed for a particular pub_id
$SqlStr="SELECT * FROM COMPANY WHERE PUB_ID=".$row['PUB_ID'];
//echo $SqlStr;
$result1 = mysql_query($SqlStr, $connection) or die("Couldn't execute select query"
The problem comes into to play when there is not a company (or state). The $row['PUB.PUB_ID'] has no value, but there is a record in the PUB table??
Really need help with the FOUR table SQL. Thanks.