如何计算个人资料浏览量?

时间:2017-08-16 04:50:25

标签: mysql sql count

这是我的表结构:

-- users
+----+--------+---------------------+----------+--------------------+
| id |  name  |       email         | password |      cookie        |
+----+--------+---------------------+----------+--------------------+
| 1  | Jack   | jack0011@gmail.com  | 098u23r8 | kj3409t34034u8rf   |
| 2  | Peter  | wanter_s@gmail.com  | k0io34r4 | m32i0949y834rf34   |
| 3  | Martin | dr.mrtn@yahoo.com   | fg345t34 | rf34r89p23r49i34   |
+----+--------+---------------------+----------+--------------------+

-- user_details
+---------+------+----------------------+----------------+
| user_id | age  |          about       |   birth_date   |
+---------+------+----------------------+----------------+
| 1       | 32   | NULL                 | NULL           |
| 2       | NULL | A senior developer   | 1992-02-10     |
| 3       | 18   | NULL                 | 1999-01-06     |
+---------+------+----------------------+----------------+

-- profile_views
+----+---------+-----------+
| id | user_id | viewer_id |
+----+---------+-----------+
| 1  | 2       | 1         |
| 2  | 1       | 2         |
| 3  | 1       | 3         |
+----+---------+-----------+
-- Unique Index: seen(user_id,viewer_id)

这是我的VIEW

CREATE VIEW `user` AS 
SELECT u.id,
       u.name,
       ud.age,
       ud.about,
       ud.birth_date
FROM users u
INNER JOIN user_details ON u.id = ud.user_id

当前输出:

-- Assuming :user_id = 1
-- SELECT * FROM user WHERE id = :user_id
+----+--------+------+----------------------+----------------+
| id |  name  | age  |          about       |   birth_date   |
+----+--------+------+----------------------+----------------+
| 1  | Jack   | 32   | NULL                 | NULL           |
+----+--------+------+----------------------+----------------+

预期输出:

+----+--------+------+----------------------+----------------+---------------+
| id |  name  | age  |          about       |   birth_date   | profile_views |
+----+--------+------+----------------------+----------------+---------------+
| 1  | Jack   | 32   | NULL                 | NULL           | 2             |
+----+--------+------+----------------------+----------------+---------------+

我该怎么做?

3 个答案:

答案 0 :(得分:2)

您必须加入个人资料视图以及

等用户详细信息
var obj = json;
for (i = 0; i < obj["panes"].length; i++) {
  var innerObj = obj["panes"][i];
  if (innerObj["type"] == "media") {
    var mediaObj = innerObj["mediaList"];
    absElement(mediaObj, innerObj["left"], innerObj["top"], innerObj["width"], innerObj["bottom"], null);
  }

}

function absElement(obj, x, y, w, h, j) {

  //for (i = 0; i < obj.length; i++) {

  if (j == null) j = 0;
  var type = obj[j]["mediaCategory"];
  let d = document.createElement(type);
  d.style.position = "absolute";
  d.style.left = x + "px";
  d.style.top = y + "px";
  d.style.width = w + "px";
  d.style.bottom = h + "px";

  //case 1 : first one is video
  if (type == 'video') {
    d.autoplay = true;
    d.src = obj[j]["mediaUrl"];
    d.preload = "none";
    d.onended = function () {
      // d.parentNode.removeChild(d);
      // i++;
      pushElements(obj, j++);
    }
  }
  else if (type == "img") {
    d.src = obj[j]["mediaUrl"];
    setTimeout(pushElements(obj, j++), obj[j]["duration"] * 1000);

    // i++;
  }

  document.body.appendChild(d);

}
//}

function pushElements(element, j) {
  absElement(element, element["left"], element["top"], element["width"], element["bottom"], j);
} 

答案 1 :(得分:1)

计算用户

CREATE VIEW `user` AS 
SELECT u.id,
   u.name,
   ud.age,
   ud.about,
   ud.birth_date,
   count(pv.user_id) AS 'Profile_View'
FROM users u
INNER JOIN user_details ud ON u.id = ud.user_id
INNER JOIN profile_views pv ON u.id = pv.user_id

答案 2 :(得分:1)

查看计数事物是高度可访问的事情。你不应该只依赖mysql。是的,您可以存储每条记录,但每次获取计数都非常昂贵。它对高度可扩展的系统也不利。您的用户群日益增加。每次扫描整个表后,mysql都要计算。

保留此数据库结构。添加一个表,您将在其中有两个字段user_id,view_count。

每次查看用途时。增加该用户的查看次数。很明显,用户ID是一个关键。

您可以使用Redis / Memcached存储查看次数。