简体   繁体   English

如何根据用户星级对mysql数据库中的用户进行过滤

[英]How to filter users in mysql database based on their star rating

So I am Currently working on a project where I need to filter active users based on the star rating from a select input field. 因此,我目前正在从事一个项目,该项目需要根据所选输入字段中的星级对活跃用户进行过滤。

Below is my users table 以下是我的用户表

| id | username | firstname | lastname | description | active |
---------------------------------------------------------------
| 2  | kay      | Albert    | Kojo     | Tall        | 1      |
| 3  | kay123   | Mary      | Thompson | Tall        | 1      |
| 4  | kay124   | Francis   | Addai    |             | 1      |

Below is my user_reviews table 下面是我的user_reviews表

| id | user_id| rating |
------------------------
| 1  |   2    |  5     |
| 2  |   3    |  3     | 

Below is my current query: 以下是我当前的查询:

$ratings = mysqli_real_escape_string($db, $_POST['rating']);

SELECT
  *,
  users.id,
  FORMAT(AVG( user_reviews.rating ), 1) AS rating_value,
  count( user_reviews.user_id ) AS review_count 
FROM
  users
  LEFT JOIN user_reviews ON users.id = user_reviews.user_id
WHERE
  users.username LIKE '%%' 
AND users.active = 1 AND (users.description != "" OR users.description IS 
  NOT NULL)
GROUP BY
  users.id 
HAVING
  $ratings <= rating_value
ORDER BY
  review_count DESC

The above query only returns users who are found in user_reviews table. 上面的查询仅返回在user_reviews表中找到的用户。 I would be very glad if someone can help me retrieve users who are in the user_reviews table and users who are not. 如果有人可以帮助我检索user_reviews表中的用户和非user_reviews表中的用户,我将感到非常高兴。

You can try this query: 您可以尝试以下查询:

SELECT 
   users.*,
   (SELECT FORMAT(AVG(rating),1) FROM user_reviews where users.id = user_reviews.user_id) AS rating_value,
   (SELECT count(user_id) FROM user_reviews where users.id = user_reviews.user_id) AS review_count
FROM
   users
WHERE 
   users.username LIKE '%%' 
   AND users.active = 1 AND (users.description != "" OR users.description IS 
   NOT NULL)
HAVING
   $rating <= rating_value
ORDER BY
   review_count DESC

It will return the output like below: 它将返回如下输出:
(Without HAVING $rating <= rating_value ) (没有HAVING $rating <= rating_value

| id | username |firstname | lastname | description | active | rating_value | review_count |
----------------------------------------------------------------------------------
| 2  | kay      | Albert   | Kojo     | Tall        | 1      | 5.0          |  1           |
| 3  | kay123   | Mary     | Thompson | Tall        | 1      | 3.0          |  1           |   
| 4  | kay134   | Francis  | Addai    |             | 1      |  NULL        |  0           |

(With HAVING $rating <= rating_value , where $rating == 4 ) (具有HAVING $rating <= rating_value ,其中$rating == 4

| id | username |firstname | lastname | description | active | rating_value | review_count |
----------------------------------------------------------------------------------
| 2  | kay      | Albert   | Kojo     | Tall        | 1      | 5.0          |  1           |

Note: 注意:
We don't need to use the GROUP_BY as the GROUP_BY will make your results only show the ones in relation. 我们不需要使用GROUP_BY因为GROUP_BY将使您的结果仅显示相关的结果。

Change 更改

 GROUP BY
   users.id 
 HAVING
   $ratings <= rating_value

To

 GROUP BY
   users.id 
 HAVING
   (rating >= $ratings or rating IS NULL )

That should solve your missing records. 那应该解决您丢失的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM