あなたはhaving
句に条件を追加することができます。
SELECT idUser, username, telephone, latitude, longitude,
(6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))) AS distance
FROM main
WHERE active = ? AND idUser != ?
HAVING distance < 5 OR username IN ('mike', 'john');
あなたはidUser
を削除する必要があるかもしれません。 。 。それがusername
に関連しているかどうかはわかりません。
EDIT:
あなたはマイクとジョンは、結果セットにあることを保証したい場合は、あなたが行うことができます:
(SELECT idUser, username, telephone, latitude, longitude,
(6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))) AS distance
FROM main
WHERE active = ? AND idUser != ? AND username NOT IN ('mike', 'john')
HAVING distance < 5
)
UNION ALL
(SELECT idUser, username, telephone, latitude, longitude,
(6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))) AS distance
FROM main
WHERE username IN ('mike', 'john')
)
または:
SELECT idUser, username, telephone, latitude, longitude,
(6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))) AS distance
FROM main
WHERE (active = ? AND idUser <> ?) OR username IN ('mike', 'john')
HAVING distance < 5 OR username IN ('mike', 'john');
かにすべての条件を移動HAVING
句:
SELECT idUser, username, telephone, latitude, longitude,
(6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))) AS distance
FROM main
WHERE (active = ? AND idUser <> ?) OR username IN ('mike', 'john')
HAVING (active = ? AND idUser <> ? AND distance < 5) OR
username IN ('mike', 'john');
はい、それは私がやっていることですが、これはうまくいきません。理由は、私はそれがアクティブであるユーザを選択すると思うからです(0と仮定します)。マイクが0としてアクティブでない場合、それは動作しません –