Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 按用户id和共享该条目的任何行选择行';事件id_Mysql_Sql_Node.js_Json - Fatal编程技术网

Mysql 按用户id和共享该条目的任何行选择行';事件id

Mysql 按用户id和共享该条目的任何行选择行';事件id,mysql,sql,node.js,json,Mysql,Sql,Node.js,Json,我有一个表格,其中包括以下格式: _______________________________ | event_id | user_id | username | |-------------------------------| | 30 | 1 | user1 | | 30 | 2 | user2 | | 30 | 3 | user3 | | 31 | 1 | user1

我有一个表格,其中包括以下格式:

 _______________________________
| event_id | user_id | username |
|-------------------------------|
|    30    |    1    |   user1  |
|    30    |    2    |   user2  |
|    30    |    3    |   user3  |
|    31    |    1    |   user1  |
|    31    |    4    |   user4  |
|    31    |    7    |   user5  |
|    32    |    3    |   user1  |
|    32    |    4    |   user4  |
|    32    |    5    |   user5  |
|_______________________________|
我考虑将用户存储为JSON:

 _______________________________________________________
| event_id |                   users                    |
|-------------------------------------------------------|
|    30    | [{"user_id": 1, "username": "user1"}, ...] |
|    31    | [{"user_id": 1, "username": "user1"}, ...] |
|    32    | [{"user_id": 5, "username": "user5"}, ...] |
|_______________________________________________________|
但我认为,当试图基于JSON值查找事件时,这会对性能造成严重影响


我希望能够查找出现
用户名
的任何事件,并返回具有相同
事件id的所有行
。这可以在单个查询中完成吗(我希望尽可能减少服务器压力),还是只执行嵌套选择?

使用
group\u concat
尝试以下操作,下面是

select
    event_id,
    concat('[', group_concat(concat('{"user_id":', user_id, ', "username":"',username,'"}')), ']') as users   
from yourTable
group by
    event_id
输出:

| event_id | users                                                                                                       |
| -------- | ----------------------------------------------------------------------------------------------------------- |
| 30       | [{"user_id":1, "username":"user1"},{"user_id":2, "username":"user2"},{"user_id":3, "username":"user3"}] |
| 31       | [{"user_id":1, "username":"user1"},{"user_id":4, "username":"user4"},{"user_id":7, "username":"user5"}] |
| 32       | [{"user_id":3, "username":"user1"},{"user_id":4, "username":"user4"},{"user_id":5, "username":"user5"}] |
我希望能够查找出现用户名的任何事件,并返回具有相同事件id的所有行

您可以使用
exists
和相关子查询:

select t.*
from mytable t
where exists (
    select 1 from mytable t1 where t1.event_id = t.event_id and t1.username = ?
)
为了提高性能,您需要在
(用户名、事件id)
上建立索引

问号表示您正在查找的
用户名


我不建议将数据存储为JSON:这会使查询更加复杂,几乎没有增加值(存储的结构跨行一致,因此非常适合于表)。

您说查询用于生成表。如果是这样,我想你想要这样的东西:

select e.*, u.user_name
from events e join 
     users u
     on e.user_id = u.user_id
where exists (select 1
              from events e2
              where e2.event_id = e.event_id and
                    e2.user_id = :user_id  -- whichever one you want
             );

如果这与您的查询一致,那么
事件(event\u id,user\u id)
上的索引将有助于提高性能。

决定只执行一个返回event\u id的嵌套选择;我最初没有这样做,因为我认为需要为其启用node mysql的多语句查询选项,但事实并非如此。

连接就足够了选择并不是那么复杂,它只需要另一个代码,但如果需要更新,它会变得复杂得多。因此,如果您真的不需要json,请不要使用itEdit编辑您的问题并显示用于生成表的查询。谢谢,但我根据您的建议尝试了几种不同的方法,仍然只能让它返回与
用户名
匹配的行。我刚刚决定做一个嵌套选择,返回
事件id
;我最初没有这样做,因为我认为需要为其启用node mysql的multiple statement querys选项,但事实证明并非如此。