MySQL 检查类别行是否存在

MySQL check if row with category exists

我在这个查询中使用了 Haversine 公式,它一直有效。目标是检查每行是否在 $deflin 值中有特定类别,看起来像 $deflin = category1, category2, category3。结果将显示 50 公里以内的行,如果它包含 $deflin 定义的任何类别。不确定如何使用 WHERE IN 或 LIKE 来解决这个问题。任何帮助表示赞赏。

MySQL 查询

$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));     

我认为您不喜欢使用 LIKE 运算符,因为它只搜索列中的模式,而您只想 return 确实 的行 有您的类别之一。相反,您应该使用 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";