English 中文(简体)
SlectT DISTINCT 多个实地搜索
原标题:SELECT DISTINCT multiple field search?
  • 时间:2010-04-03 00:09:47
  •  标签:
  • php
  • mysql

I m试图搜索多个领域(zc_city, zc_zip和zc_state),与用户单一价值投入相匹配。 这三栏应列入结果。 这里我要说的是:

$q = strtolower($_GET["q"]);
if (!$q) return;

$sql = "SELECT DISTINCT zc_city AS zcity FROM search_zipcodes WHERE zc_city LIKE  $q% ";

$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) {
    $zcity = $rs[ zcity ];
    echo "$zcity
";
}

该表的结构如下:

CREATE TABLE search_zipcodes (
  zc_zip VARCHAR(5),
  zc_lat FLOAT,
  zc_lon FLOAT,
  zc_city VARCHAR(80),
  zc_state CHAR(2) 
);
最佳回答

退还至少一栏的配量,使用<代码> OR:

SELECT zc_city AS city, zc_state AS state, zc_zip AS zip
  FROM search_zipcodes
  WHERE zc_city LIKE :city OR zc_zip=:zip

作为一个准备的问题,这看起来是:

// connect to the DB. Should be in its own function to isolate credentials.
$db = new PDO(...);

// get the cities. Should be in a function/method to access DB (the data access layer).
$findPlace = $db->prepare("SELECT zc_city AS city, zc_state AS state, zc_zip AS zip
      FROM search_zipcodes
      WHERE zc_city LIKE :city OR zc_zip=:zip");
$places = $findPlace->execute(array( :city  => $_REQUEST[ q ] .  % , 
                                     :zip  => $_REQUEST[ q ]));

// display places. Should be in a method of a view class.
?>
  <ul>
    <?php foreach ($places as $place) {
       echo "<li>$place[city], $place[state] $place[zip]</li>
";
    } ?>
  </ul>

说明提出了三项不同的任务,包括评论。 每一组应属于单独类别,头两组在第三层(如模型-意见或

问题回答

这好像你只是要求获得其他资源:

$sql = "SELECT zc_city, zc_state, zc_zip FROM search_zipcodes WHERE zc_city LIKE  $q%  OR zc_state LIKE  $q%  OR zc_zip LIKE  $q%  ";

我仍不敢肯定你已经指出这一表上的主要关键。 如果是Zip或Zip+City,那么你就不需要使用。 如果在同一个城市、州和西佩有多个条目,那么你可以使用<代码>。 依次:

$sql = "SELECT zc_city, zc_state, zc_zip FROM search_zipcodes WHERE zc_city LIKE  $q%  OR zc_state LIKE  $q%  OR zc_zip LIKE  $q%  GROUP BY zc_city, zc_state, zc_zip ";

http://www.ohchr.org。 你可以更深入地探讨从询问中取得结果,但你可以尝试像:

while ( $rs = mysql_fetch_assoc($rsd) ){
    $array[] = $row;
}

这把结果排入一个阵列,阵列的每一条目都是一阵列的数值,而最后的数值总是空的。

while ( ($resultArray[] = mysql_fetch_assoc($rsd) ) || array_pop( $resultArray ) );




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签