Mysql查询过滤器

时间:2012-07-01 01:50:18

标签: mysql sql

所以我试图从我的sql数据库中获取数据,然后通过某些与我可能要生成的报告生成相关的变量对其进行排序。

<?
$sql = 'SELECT DISTINCT jsfdName.baseData AS Name, jsfdAddr.baseData as Address, jsfdZip.baseData AS Zip, jsfdCity.baseData AS City, jsfdState.baseData AS State,jsfdPhone.baseData AS Phone, jsfdPerson.baseData AS Person, jsfdContacted.baseData AS Contacted
FROM jos_sobipro_field_data AS jsfdName
JOIN jos_sobipro_field_data AS jsfdAddr USING(sid)
JOIN jos_sobipro_field_data AS jsfdZip USING(sid)
JOIN jos_sobipro_field_data AS jsfdCity USING(sid)
JOIN jos_sobipro_field_data AS jsfdState USING(sid)
JOIN jos_sobipro_field_data AS jsfdPhone USING(sid)
JOIN jos_sobipro_field_data AS jsfdPerson USING(sid)
JOIN jos_sobipro_field_data AS jsfdContacted USING(sid)
WHERE jsfdName.fid = 36 AND jsfdAddr.fid = 37 AND jsfdZip.fid=38 AND jsfdCity.fid = 39 AND jsfdState.fid = 41 AND jsfdPhone.fid = 46 AND jsfdPerson.fid = 62 AND jsfdContacted.fid = 61 AND sid > 905';
$rs_result = mysql_query($sql) or die (mysql_error());
$ttl=mysql_num_rows($rs_result);
?>

<center><H2><?=$sitename;?></h2>
<h3>Business Information</h3></center>
<center>Total Entries in Report:&nbsp;<?=$ttl;?></center>
<table border="1" ALIGN = CENTER>
<tr><th>Name</th><th>Address</th><th>Zip</th><th>City</th><th>State</th><th>Phone</th><th>Sales Person</th><th>Contact Method</th></tr>
<?
while ($row = mysql_fetch_assoc($rs_result)) {
?>
  <tr>
  <td><? echo $row[Name]; ?></td>
  <td><? echo $row[Address]; ?></td>
  <td><? echo $row[Zip]; ?></td>
  <td><? echo $row[City]; ?></td>
  <td><? echo $row[State]; ?></td>
  <td><? echo $row[Phone]; ?></td>
  <td><? echo $row[Person]; ?></td>
  <td><? echo $row[Contacted]; ?></td>
  </tr>
<? }; ?></table>

所以我期待只显示名字叫Justin,Jeremy等的销售人员。所以我想我必须在我的where子句中添加另一个条件,就像AND Person =&#34; Justin&# 34; .....但那不起作用。有什么想法吗?

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

你在jsfdPhone.fid = 46之后得到了'AND AND'。

答案 1 :(得分:0)

我在WHERE子句的末尾添加了另一个AND。它不可能在结束之前,否则它会影响表格并给我错误。所以现在where子句看起来像这样......

WHERE jsfdName.fid = 36 AND jsfdAddr.fid = 37 AND jsfdZip.fid=38 AND jsfdCity.fid = 39 AND jsfdState.fid = 41 AND jsfdPhone.fid = 46 AND jsfdPerson.fid = 62 AND jsfdContacted.fid = 61 AND sid > 905 AND jsfdPerson.baseData = "Justin"';

再次感谢你们