我正在構建保姆列表的搜索頁面,用戶可以根據工資,距離和評分過濾結果。一切正常,但即使只有兩個人或一個人,我也能得到三到四個結果,但我無法弄清楚。如何在mysql中進行分組而不顯示重複項?
這裏是我的查詢:
SELECT `users`.`id` AS `user_id`,
`user_wage_preferences`.`one_child` AS `wage`,
`user_contact_informations`.`lat`,
`user_contact_informations`.`lng`,
`user_reviews`.`rating`,
Sum(user_reviews.recommended) AS recommended,
(6371 * Acos(Cos(Radians(59.448355500000005)) * Cos(Radians(lat)) * Cos(
Radians(lng) - Radians(24.7406023)) +
Sin(Radians(59.448355500000005)) * Sin(
Radians(lat))))
AS distance
FROM `users`
LEFT JOIN `user_reviews`
ON `users`.`id` = `user_reviews`.`nanny_id`
INNER JOIN `user_wage_preferences`
ON `users`.`id` = `user_wage_preferences`.`user_id`
INNER JOIN `user_contact_informations`
ON `users`.`id` = `user_contact_informations`.`user_id`
WHERE `users`.`role` = ?
AND `users`.`account_status` = ?
AND `users`.`id` IN (?, ?)
GROUP BY `user_id`,
`user_contact_informations`.`lat`,
`user_contact_informations`.`lng`,
`user_reviews`.`rating`,
`user_wage_preferences`.`one_child`
HAVING `recommended` > ?
ORDER BY `recommended` DESC
在我的JSON響應我看到這一點:
[
{
"user_id":1,
"wage":"6",
"lat":59.44,
"lng":24.74,
"rating":5,
"recommended":"1",
"distance":0.92971285651468
},
{
"user_id":4,
"wage":"4",
"lat":59.46,
"lng":24.83,
"rating":5,
"recommended":"1",
"distance":5.2153552636895
},
{
"user_id":1,
"wage":"6",
"lat":59.44,
"lng":24.74,
"rating":4,
"recommended":"0",
"distance":0.92971285651468
},
{
"user_id":4,
"wage":"4",
"lat":59.46,
"lng":24.83,
"rating":1,
"recommended":"1",
"distance":5.2153552636895
}
]
正如你看到的,我得到user_id 1
和user_id 4
兩次。我不知道如何刪除user id
重複或組。
我在做什麼錯在這裏?我如何顯示用戶一次? 我正在使用laravel作爲框架。
使用'不同('users'.'id')作爲id'在SELECT語句 –
你得到了多個結果,因爲你也通過「評級」進行分組,所有重複記錄都不同。刪除它刪除重複的東西 –