您可以將條件添加到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
有關。
編輯:
如果你想保證邁克和約翰是一個結果集,你可以這樣做:
(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),並且如果mike沒有活躍爲0,則它將不起作用 –