直接去获取代码。
用户表
+----+------------------+
| id | username |
+----+------------------+
| 1 | becker.cornelius |
| 2 | claudine29 |
| 3 | dion37 |
| 4 | zkozey |
| 5 | kasandra59 |
+----+------------------+
列出表格
+----+---------------------------+
| id | name |
+----+---------------------------+
| 1 | List 1 |
| 2 | List 2 |
| 3 | List 3 |
| 4 | List 4 |
| 5 | List 5 |
+----+---------------------------+
lists_data表
+-----+---------+----------------------------------------------------------------+
| id | list_id | user_id |
+-----+---------+----------------------------------------------------------------+
| 421 | 4 | 1,2,3,503433,707152,603114,342531,414463,977165 |
+-----+---------+----------------------------------------------------------------+
data
列包含 100 万用户。以下是我的代码
select users.id, email, mobilenumber, firstname, surname, created_at
from users
inner join lists_data on find_in_set(users.id,lists_data.user_id) > 0 where lists_data.list_id = 97
上面的代码可以工作,唯一的问题是速度慢而且我无法使用分页,</p>
我尝试过很多方法。他们都没有按预期工作。
顺便说一句,我无法标准化lists_data表,因为它可能包含每个列表数百万条记录。
最佳答案
您对关系数据库使用了错误的数据结构。您的 lists_data
表应包含 list_id
和 user_id
(单数)列。示例行将是多行:
id list_id user_id
1 97 1
2 97 2
3 97 3
4 97 503433
. . .
这称为联结表。而且,虽然规模更大,但 SQL 发现使用此类表要简单得多。
关于mysql - FIND_IN_SET 具有限制偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36425595/