MySQL检查是否存在具有类别的行

时间:2015-05-23 04:50:31

标签: mysql sql sql-like where-in

我正在使用此查询的Haversine公式,它一直有效。目标是检查每行是否具有$ deflin值的某些类别,其类似于$ deflin = category1,category2,category3。结果将显示50公里内的行,如果它包含$ deflin定义的任何类别。不知道如何使用WHERE IN或LIKE来解决这个问题。任何帮助表示赞赏。

MySQL for query

$awaka = "SELECT *,
( 6371 * acos( cos( radians(?) ) * cos( radians(job_latitude) ) *   
cos(radians(?) - radians(job_longitude) ) + sin( radians(?) ) *   
sin( radians(job_latitude) ) ) ) AS distance FROM job, users   
WHERE job.listee_id = users.user_id AND job.job_category LIKE ?   
HAVING distance < 50";

$result = $this->db->query($awaka, array($conlat, $conlong, $conlat, $deflin));     

1 个答案:

答案 0 :(得分:2)

我认为您不想使用LIKE运算符,因为它只搜索列中的模式而您只是不想返回真的拥有您的某个行的行类别。相反,您应该使用IN子句来检查作业是否具有您的某个类别:

// contains the id for each category
$categories = array(1, 4, 5, 6);
$deflin = implode(',', $categories);

// if job_category is a text column you could do like this instead
$categories = array('category1', 'category2', 'category3');
$deflin = implode(',', $categories);    

$awaka = "SELECT *,
( 6371 * acos( cos( radians(?) ) * cos( radians(job_latitude) ) *   
cos(radians(?) - radians(job_longitude) ) + sin( radians(?) ) *   
sin( radians(job_latitude) ) ) ) AS distance FROM job, users   
WHERE job.listee_id = users.user_id AND job.job_category IN ($deflin)   
HAVING distance < 50";