MySQL限制相同“用户”值的最大行数

时间:2021-06-07 13:02:28

How can I limit in MySQL maximum rows for the same "user" value?

如何针对相同的“用户”值限制MySQL的最大行数?

For example, I have table with columns

例如,我有表格列

id | user | data

and I would like to limit maximum rows count to 5 for each user (the same "user" value).

我想将每个用户的最大行数限制为5(相同的“用户”值)。

My idea is to use transactions (InnoDB):

我的想法是使用交易(InnoDB):

start transaction
insert row with user = "XXX"
count rows, where user = "XXX"
if count < 5 commit else rollback

Is my idea good or exist also another (better) solution?

我的想法是好还是存在另一个(更好的)解决方案?

1 个解决方案

#1


1  

I think that it's ok, You may want to make a

我认为没关系,你可能想做一个

select count(1) from table where user='XXX'

and check the count before performing the insert.

并在执行插入之前检查计数。

#1


1  

I think that it's ok, You may want to make a

我认为没关系,你可能想做一个

select count(1) from table where user='XXX'

and check the count before performing the insert.

并在执行插入之前检查计数。