简体   繁体   English

如何在1个表中插入相同的数据并将其限制为10

[英]How to insert same data in 1 table and Limit it by 10

I am working in a "Recently view by user" script. 我正在使用“最近用户查看”脚本。 I have 3 tables, tbl_user, tbl_articles and tbl_recently_viewed. 我有3个表, tbl_user, tbl_articles and tbl_recently_viewed. My tbl_recently_viewed has 3 fields, user_id(from tbl_user),article_id(from tbl_articles) and date, and i made 2 primary keys, user_id and article_id. 我的tbl_recently_viewed有3个字段, user_id(from tbl_user),article_id(from tbl_articles) and date,我制作了2个主键, user_id and article_id. My problem is i want to limit a certain user_id to be inserted in tbl_recently_viewed by 10. Is there a way i can do this? 我的问题是我想限制某个user_id插入到tbl_recently_viewed中10.有没有办法可以做到这一点?

Thanks. 谢谢。

I think that you need a trigger to do what you want to do. 我认为你需要一个触发器去做你想做的事情。 If you are only inserting rows, then you can use an insert trigger to remove the oldest. 如果只插入行,则可以使用插入触发器删除最旧的行。

The following is intended just as an example: 以下仅作为示例:

CREATE TRIGGER t_keep10 after INSERT ON t
  FOR EACH ROW BEGIN
     if (10 > (select count(*) from t where t.user_id = new.user_id)) then
         delete from t where t.article_id = (select a from (select article_id as a from t t2 where t2.user_id = t.user_id order by date desc limit 1))
     end if;
  END;
|

To delete the oldest item for a user from tbl_recently_viewed : 要从tbl_recently_viewed删除用户的最旧项目:

DELETE v.* FROM tbl_recently_viewed v
JOIN (SELECT MIN(date) mindate
      FROM tbl_recently_viewed
      WHERE user_id = ?) r
ON v.date = r.mindate
WHERE user_id = ?

You could delete rows after 10 with this query: 您可以使用此查询删除10之后的行:

delete from tbl_recently_viewed where user_id=1 and date <= (select max(date) from (select date from tbl_recently_viewed where user_id=1 order by date desc limit 10,999) as tmp);

or just delete oldest row with query: 或者只是用查询删除最旧的行:

delete from tbl_recently_viewed where user_id=1 order by date limit 1;

暂无
暂无

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

相关问题 如何在Laravel中将数据插入具有相同模型列的数据透视表中? - How to insert data into a pivot table with columns belonging to same model in Laravel? 如何通过mysql或python将数据插入另一个具有相同id的表中 - How to insert data into another table with same id by mysql or python 如何根据复选框在不同的表中插入相同的数据? mysql php - how to insert same data in different table based on checkbox ? mysql php 如何获取数据并插入到同一数据库中的另一个表中? - How to fetch data and insert into another table in same database? 如何创建表并在其中插入数据,但是如果已经存在相同名称的表,则只需插入数据即可,不再创建表 - how to create table and insert data inside it but if there is table already the same name just insert data and do not create table anymore 当用户将数据输入到两个不同的页面时,如何将数据插入到同一表和同一行中 - How to insert data into the same table and in same row when the user will enter the data into two different pages 同时将数据插入父表和子表 - insert data to parent and child table at the same time 将3个具有相同字段的表中的数据插入1个表中 - insert data from 3 tables with the same fields into 1 table Select 来自不同表的数据限制10,而其他表限制5 - Select data from different tables limit 10, while the other table limit 5 如何在表的同一列的多行中从单一格式插入相同名称的多个数据 - How to insert multiple data of same name from single form in multiple row of same column of table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM