MySql 选择Where 或Where not...而许多其他“与”都可以选择和“Ors”

发布于 2024-12-08 15:37:06 字数 1049 浏览 1 评论 0原文

我做错了什么?

我想选择仅限天主教的教堂,但它正在选择更多。是因为拉链吗?他们工作得很好。

$Zips = array(60618,60625,60647,60641,60613,60657,60640,60659,60614,60639,60622,60630,60660,60651,60642,60645,60712,60612,60646,60624,60610,60626,60674,60654,60661,60644,60634,60606,60607,60707,60302,60611,60202,60602,60601,60706,60303,60603,60604,60301,60701,60656,60076,60204,60209,60304,60077,60623,60608,60305,60203,60605,60631,60664,60668,60669,60670,60673,60675,60677,60678,60680,60681,60684,60685,60686,60687,60688,60690,60691,60693,60694,60696,60697,60699,60689,60695,60171,60201,60682,60208,60053,60804,60714,60616,60130,60161,60153,60176,60068,60160,60029,60141,60091,60402,60632,60131,60290,60165,60609,60043,60164,60546);
$ZipCount = count($Zips);

for ($i = 0; $ZipCount > $i; $i++) {
    if ($i == 0) {
        $TheZips = "Zip LIKE '%$Zips[$i]%'";
    }
    else {
        $TheZips .= "OR Zip LIKE '%$Zips[$i]%'";
    }
}

$sql = mysql_query("SELECT DISTINCT * FROM info WHERE Catholic and '%true%' OR $TheZips");
$result = mysql_num_rows($sql);

What am I doing wrong?

I want to select Churches that are Catholic Only but it is selecting more. Is it because of the Zips? they work great.

$Zips = array(60618,60625,60647,60641,60613,60657,60640,60659,60614,60639,60622,60630,60660,60651,60642,60645,60712,60612,60646,60624,60610,60626,60674,60654,60661,60644,60634,60606,60607,60707,60302,60611,60202,60602,60601,60706,60303,60603,60604,60301,60701,60656,60076,60204,60209,60304,60077,60623,60608,60305,60203,60605,60631,60664,60668,60669,60670,60673,60675,60677,60678,60680,60681,60684,60685,60686,60687,60688,60690,60691,60693,60694,60696,60697,60699,60689,60695,60171,60201,60682,60208,60053,60804,60714,60616,60130,60161,60153,60176,60068,60160,60029,60141,60091,60402,60632,60131,60290,60165,60609,60043,60164,60546);
$ZipCount = count($Zips);

for ($i = 0; $ZipCount > $i; $i++) {
    if ($i == 0) {
        $TheZips = "Zip LIKE '%$Zips[$i]%'";
    }
    else {
        $TheZips .= "OR Zip LIKE '%$Zips[$i]%'";
    }
}

$sql = mysql_query("SELECT DISTINCT * FROM info WHERE Catholic and '%true%' OR $TheZips");
$result = mysql_num_rows($sql);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天荒地未老 2024-12-15 15:37:06
$str_zips = implode(', ', $Zips);

$sql = 
mysql_query("SELECT DISTINCT * FROM info WHERE Catholic=true AND Zip IN ({$str_zips})");
$str_zips = implode(', ', $Zips);

$sql = 
mysql_query("SELECT DISTINCT * FROM info WHERE Catholic=true AND Zip IN ({$str_zips})");
你的呼吸 2024-12-15 15:37:06

恕我直言,你应该使用:

for($i=0;$ZipCount>$i;$i++)
{
  if($i==0){  $TheZips= "Zip = '$Zips[$i]'"; }
  else {  $TheZips.= "OR Zip = '$Zips[$i]'";} 
}    

$sql = mysql_query(
  "SELECT DISTINCT * FROM info WHERE Catholic=true AND ($TheZips)");

for($i=0;$ZipCount>$i;$i++)
{
  if($i==0){  $TheZips= "$Zips[$i]"; }
  else {  $TheZips.= ",$Zips[$i]";} 
}    

$sql = mysql_query(
  "SELECT DISTINCT * FROM info WHERE Catholic=true AND Zip IN ($TheZips)");

IMHO you should use:

for($i=0;$ZipCount>$i;$i++)
{
  if($i==0){  $TheZips= "Zip = '$Zips[$i]'"; }
  else {  $TheZips.= "OR Zip = '$Zips[$i]'";} 
}    

$sql = mysql_query(
  "SELECT DISTINCT * FROM info WHERE Catholic=true AND ($TheZips)");

or

for($i=0;$ZipCount>$i;$i++)
{
  if($i==0){  $TheZips= "$Zips[$i]"; }
  else {  $TheZips.= ",$Zips[$i]";} 
}    

$sql = mysql_query(
  "SELECT DISTINCT * FROM info WHERE Catholic=true AND Zip IN ($TheZips)");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文