复杂的MySQL查询需要帮助-范围非常狭窄
以下是我介绍的表格及其内容:复杂的MySQL查询需要帮助-范围非常狭窄,mysql,jdbc,Mysql,Jdbc,以下是我介绍的表格及其内容: 用户: 内容: +----+-------------------+----------------------+--------+ | id | public_key_path | email | pbox | +----+-------------------+----------------------+--------+ | 33 | /path/to/pubkey1/ | FirstUser@email.com |
用户: 内容:
+----+-------------------+----------------------+--------+
| id | public_key_path | email | pbox |
+----+-------------------+----------------------+--------+
| 33 | /path/to/pubkey1/ | FirstUser@email.com | Pbox01 |
| 34 | /path/to/pubkey2/ | SecondUser@email.com | Pbox02 |
| 35 | /path/to/pubkey3/ | ThirdUser@email.com | Pbox03 |
+----+-------------------+----------------------+--------+
+-----+-----------------+----------+
| id | file_path | owner_id |
+-----+-----------------+----------+
| 104 | /path/to/file1/ | 33 |
| 105 | /path/to/file2/ | 34 |
| 106 | /path/to/file3/ | 35 |
| 107 | /path/to/file4/ | 33 |
| 108 | /path/to/file5/ | 33 |
| 109 | /path/to/file6/ | 34 |
+-----+-----------------+----------+
档案:
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| file_path | varchar(100) | NO | UNI | NULL | |
| owner_id | int(11) | NO | MUL | NULL | |
+-----------+--------------+------+-----+---------+----------------+
内容:
+----+-------------------+----------------------+--------+
| id | public_key_path | email | pbox |
+----+-------------------+----------------------+--------+
| 33 | /path/to/pubkey1/ | FirstUser@email.com | Pbox01 |
| 34 | /path/to/pubkey2/ | SecondUser@email.com | Pbox02 |
| 35 | /path/to/pubkey3/ | ThirdUser@email.com | Pbox03 |
+----+-------------------+----------------------+--------+
+-----+-----------------+----------+
| id | file_path | owner_id |
+-----+-----------------+----------+
| 104 | /path/to/file1/ | 33 |
| 105 | /path/to/file2/ | 34 |
| 106 | /path/to/file3/ | 35 |
| 107 | /path/to/file4/ | 33 |
| 108 | /path/to/file5/ | 33 |
| 109 | /path/to/file6/ | 34 |
+-----+-----------------+----------+
加密的\u对称\u密钥:
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| key_path | varchar(100) | YES | UNI | NULL | |
| file_id | int(11) | NO | MUL | NULL | |
| user_id | int(11) | NO | MUL | NULL | |
+----------+--------------+------+-----+---------+----------------+
内容:
+-----+---------------------+---------+---------+
| id | key_path | file_id | user_id |
+-----+---------------------+---------+---------+
| 106 | /path/to/key1forU1/ | 104 | 33 |
| 107 | /path/to/key2forU2/ | 105 | 34 |
| 108 | /path/to/key3forU3/ | 106 | 35 |
| 109 | /path/to/key4forU1/ | 107 | 33 |
| 110 | /path/to/key5forU1/ | 108 | 33 |
| 111 | /path/to/key6forU2/ | 109 | 34 |
| 112 | /path/to/key3forU1/ | 106 | 33 |
| 113 | /path/to/key2forU1/ | 105 | 33 |
| 114 | /path/to/key6forU1/ | 109 | 33 |
+-----+---------------------+---------+---------+
这是一个用于存储使用加密对称密钥(ESK)加密的文件路径的服务器的新手数据库。如果存在key\u path
/path/to/key1forU1/
,则表示为user1的file1存储密钥。我需要一个查询,当给定文件路径(例如“/path/to/file1/”)时,该查询将列出有权访问此文件的所有用户。
如果您需要更多信息,请告诉我,我会立即提供。
我知道这是一个非常局部化的问题,但我已经思考了一个多小时了,现在我在思考这个问题时遇到了可怕的问题 事先非常感谢 编辑: 我需要的最后一组数据是,
“/path/to/file1/”
将返回
FirstUser@email.com 但当给出示例时
/路径/to/file2/' 将返回
FirstUser@email.com
SecondUser@email.com
因为我想这就是你要找的 因为这将为您提供所有使用
文件的电子邮件\u path='/path/to/file6/'
,所有者的电子邮件除外
SELECT su.email
FROM encrypted_symmetric_keys AS c
INNER JOIN users AS su ON su.id = c.user_id
INNER JOIN files AS f ON f.id = c.file_id AND f.owner_id <> c.user_id
WHERE f.file_path = '/path/to/file6/'
选择su.email
从加密的\u对称\u密钥作为c
作为su ON su.id=c.user\u id的内部用户加入
将文件作为f ON f.id=c.file\u id和f.owner\u id c.user\u id进行内部连接
其中f.file_path='/path/to/file6/'
只需加入所需的表即可。如果我正确理解了您的问题,要列出用户的电子邮件,请使用类似以下内容:从用户选择电子邮件加入所有者上的文件\u id=users.id加入用户上的加密\u对称\u密钥。id=user\u id和files.id=file\u id where files.file\u path=“/path/to/file1/”您需要的最终数据集是什么?你需要每个键路径的所有用户id的列表吗?是的,我一直在尝试,但我从来没有让它完全按照我想要的方式工作。您的工作方式比我的任何迭代都要好,但只显示一封电子邮件,即给定文件的所有者(所有者id)。我需要它来显示每个有权访问它的人,或者除了所有者以外的所有人(不管是谁,两者都适用于我的目的)。因此,对于/path/to/file1/
来说,应该是FirstUser@email.com
,但对于file3,它将是两封电子邮件,第三和第一个用户。我使用所需的最终集进行编辑。现在看起来不错。可能会更改最后一个文件路径的位置,而不是键路径,因为输入是文件路径。这似乎是可行的,这不是我能想到的。什么意思?另外,修改它以显示包括所有者电子邮件在内的所有电子邮件是否很难?乍一看我做不到,但我会努力练习。或者!=我很高兴它对你有用