简体   繁体   English

如何在mysql中使用SUM和GROUP BY更新包含在另一个查询中的记录

[英]How can i update the Records included in another query using SUM and GROUP By in mysql

I am having a mysql table 我有一个mysql表

content_votes_tmp content_votes_tmp

+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| up         | int(11)          | NO   | MUL | 0       |                |
| down       | int(11)          | NO   |     | 0       |                |
| ip         | int(10) unsigned | NO   |     | NULL    |                |
| content    | int(11)          | NO   |     | NULL    |                |
| datetime   | datetime         | NO   |     | NULL    |                |
| is_updated | tinyint(2)       | NO   |     | 0       |                |
| record_num | int(11)          | NO   | PRI | NULL    | auto_increment |
+------------+------------------+------+-----+---------+----------------+

surfers can vote up or vote down on posts ie content, a record gets inserted everytime a vote is given same as rating , in the table along with other data like ip , content id 冲浪者可以对帖子(例如内容) vote upvote down ,每当表中的等级与Rate相同时,就会插入一条记录以及其他数据,如ipcontent id

Now i am trying to create cronjob script in php which will SUM(up) and SUM(down) of votes 现在,我正在尝试在php中创建cronjob脚本,该脚本的SUM(up)SUM(down)票数

like this, 像这样,

mysqli_query($con, "SELECT  SUM(up) as up_count,  SUM(down) as down_count, content FROM `content_votes_tmp` WHERE is_updated = 0 GROUP by content")

and then by using while loop in php i can update the main table for the specific content id, 然后通过在php中使用while循环,我可以针对特定的content ID更新主表,

but i would like to set the records which are part of SUM to be marked as updated ie SET is_updated = 1 , so the same values wont get summed again and again. 但是我想设置SUM的记录,将其标记为已更新,即SET is_updated = 1 ,因此相同的值不会一次又一次地求和。

How can i achieve this ? 我怎样才能做到这一点? using mysql query ? 使用mysql查询? and work on same data set as , every second/milisecond the records are getting inserted in the table ,. 并处理与相同的数据集,每秒钟/毫秒将记录插入表。


i can think of another way of achieving this is by getting all the non-updated records and doing sum in the php and then updating every record. 我可以想到另一种实现此目的的方法,就是获取所有未更新的记录并在php中做总和,然后更新每条记录。

The simplest way would probably be a temporary table. 最简单的方法可能是临时表。 Create one with the record_num values you want to select from; 用您要选择的record_num值创建一个;

CREATE TEMPORARY TABLE temp_table AS
  SELECT record_num FROM `content_votes_tmp` WHERE is_updated = 0;

Then do your calculation using the temp table; 然后使用临时表进行计算;

SELECT  SUM(up) as up_count,  SUM(down) as down_count, content 
FROM `content_votes_tmp` 
WHERE record_num IN (SELECT record_num FROM temp_table) 
GROUP by content

Once you've received your result, you can set is_updated on the values you just calculated over; 收到结果后,可以在刚刚计算出的值上设置is_updated;

UPDATE `content_votes_tmp`
SET is_updated = 1
WHERE record_num IN (SELECT record_num FROM temp_table)

If you want to reuse the connection to do the same thing again, you'll need to drop the temporary table before creating it again, but if you just want to do it a single time in a page, it will disappear automatically when the database is disconnected at the end of the page. 如果要重用连接以再次执行相同的操作,则需要在再次创建临时表之前删除该临时表,但是如果您只想在页面中单次执行该临时表,则该临时表将在数据库中自动消失在页面末尾断开连接。

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

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