0

I have no idea How can it be done or is it possible In Query.? Can be done in PHP.? If So any Idea or examples.

Here I have 2 tables.

Review

id    pid   published
--    ---   ---------
 1     12           0
 2     14           1
 3     16           1

Rating

id   rid   ratings                 sum
--   ---   ---------------------   -------
 1     2   4,5,3,3,3.5,5,5,5,4,5   42.5000
 2     4   4,5,3,3,3.5,5,5,5,4,5   42.5000
 3     1   5,5,4,4,5,5,4,4,4,4     44.0000

What I would Like to Do is

  1. Update the Rating table based on Review.id = Rating.rid.
  2. Update ratings field where ever Is find 3 or 2 or 1 update to 4 like example this 4, 5, 3, 3, 3.5, 5, 5, 5, 4, 5 should be 4, 5, 4, 4 ,4, 5, 5, 5, 4, 5
  3. Based On update ratings field SUM field should Automatic Update the Sum of the rating values. Example this 42.5000 should be 45.0000

I tried to explain my question if it is not clear please ask me to Modify or give you more detail.

My tables are exact same as example. ratings field has text data type. There Is no Another tables for ratings.

Any helps will be highly appreciate.

Thanks You.

4

2 回答 2

2

您应该重新设计您的数据库结构并将评级值存储在另一个表中,例如,使用这样的结构

id | review_id | value

然后你会很容易地做所有这些事情。

如果这种更改是不可能的,您可能应该在 PHP 中进行。我的意思是,您查询所有必要的数据,在数组ratings中展开,并在数组上进行操作。

但我强烈建议将评级移到另一个表中。

于 2013-06-10T15:25:19.943 回答
1
  • 选择评级行 ( SELECT * FROM Rating WHERE rid=YOUR_RID)

  • 在 PHP ( mysqli_fetch(),PDOStatement->fetch()等)中获取数据

  • 对于转换字符串ratings到数组使用explode函数

  • 对于与数组的交互,您可以使用foreach循环。

    $总和 = 0;

    foreach ($array as &$rating) {

    如果($评级<4)$评级=4;

    $sum+=$评分;

    }

于 2013-06-10T15:45:11.790 回答