如何从ID到ID创建MySQL语句

时间:2022-11-25 20:01:28

Example I have 500 users and every 50 users manage by one moderator.

示例I有500个用户,每50个用户由一个主持人管理。

Let's say moderator_id = 1 can edit/manage user from user_id 1 until 50. What is the statement should I use?

假设moderator_id = 1可以编辑/管理用户从user_id 1到50.我应该使用什么语句?

SELECT * FROM users
  WHERE 
  user_id `what should i use here?` 
  AND 
  moderator_id = '1';

Let me know..

让我知道..

2 个解决方案

#1


3  

Use the BETWEEN operator, like this:

使用BETWEEN运算符,如下所示:

SELECT *
FROM users
WHERE user_id BETWEEN 1 AND 50
AND moderator_id = '1';

#2


0  

I'm prefer to use an additional column with the moderator_id. In that case you can dynamically change user's set for each moderators without changing anything in the code.

我更喜欢使用moderator_id的附加列。在这种情况下,您可以动态更改每个主持人的用户集,而无需更改代码中的任何内容。

To initially define user's set per moderator use query like that:

要初步定义每个主持人的用户集,请使用以下查询:

UPDATE users
SET moderator_id = 1
WHERE user_id BETWEEN 1 AND 50

To select users by moderator use this query:

要通过主持人选择用户,请使用以下查询:

SELECT *
FROM users
WHERE moderator_id = 1

#1


3  

Use the BETWEEN operator, like this:

使用BETWEEN运算符,如下所示:

SELECT *
FROM users
WHERE user_id BETWEEN 1 AND 50
AND moderator_id = '1';

#2


0  

I'm prefer to use an additional column with the moderator_id. In that case you can dynamically change user's set for each moderators without changing anything in the code.

我更喜欢使用moderator_id的附加列。在这种情况下,您可以动态更改每个主持人的用户集,而无需更改代码中的任何内容。

To initially define user's set per moderator use query like that:

要初步定义每个主持人的用户集,请使用以下查询:

UPDATE users
SET moderator_id = 1
WHERE user_id BETWEEN 1 AND 50

To select users by moderator use this query:

要通过主持人选择用户,请使用以下查询:

SELECT *
FROM users
WHERE moderator_id = 1