SQLite - 对同一数据/列的多个条目进行选择
我对 SQLite 有点陌生,我对我的数据库设计有一个轻微的困境。我会解释一下。假设您和您的朋友在互联网上使用许多不同的昵称(每人可以多个)。我们这里拥有的是此人的 ID 以及此人使用的所有昵称的列表。这是一个大列表中的单个条目。这里的目标是使用 SQLite 存储此数据,这样 SELECT 语句可用于获取包含指定昵称的所有条目。
我考虑过创建一个包含两列的表,第一列是 ID(主键),另一列是在 CSV 格式。然而,在这个例子中,我不知道如何编写 select 语句来搜索和匹配 CSV 中存储的昵称。
有想法吗?
I am a bit new to SQLite, and I am having a slight dilemma about my database design. I'll explain. Suppose you and your friends use a number of different nicknames on the Internet (can be more than one per person). What we have here is an ID of the person and a list of all nicknames that this person uses. This is a single entry in a large list. The goal here is to store this data by using SQLite in a way the SELECT statement can be used to fetch all entries that contain the specified nickname.
I have thought about creating a table with two columns, first being the ID (primary key) and the other being a TEXT that holds nicknames in a CSV format. However in this example I don't know how to write the select statement to search and match nicknames stored in CSV.
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于初学者来说,这就是您所拥有的
,但我强烈建议使用一个名称表和一个使用的昵称表,以便昵称具有对名称的引用。
您会发现此方案将为您提供更多控制来编辑条目、删除条目等。
使用
INNER JOIN
进行查询:或嵌套查询:
这两者相当于(在本例中)指定使用
WHERE
子句进行连接。它有效,但形式很差(它不像INNER JOIN
那么清晰):For starters, here is what you have
But I would strongly suggest having a table for Names, and a table for Nicknames used, such that the Nickname has a reference to Names.
You will find this scheme will give you more control to edit entries, remove them, etc.
Query it with either an
INNER JOIN
:Or a nested query:
The two are equivalent (in this case) to specifying the join with a
WHERE
clause. It works, but it's poor form (it's not as clear asINNER JOIN
):为什么不只使用下表:
Person(列:person_id,person_name)
Nickname(列:nickname_id,nickname)
Person_Nickname(列:person_id,nickname_id)
然后创建从 Person 到 Person_Nickname 以及从 Nickname 到 Person_Nickname 的外键。这使得某个人可以拥有任意多个昵称。
然后,要查找与给定昵称匹配的所有人员,您可以编写:
Why not just have the following tables:
Person (columns: person_id, person_name)
Nickname (columns: nickname_id, nickname)
Person_Nickname (columns: person_id, nickname_id)
Then you create foreign keys from Person to Person_Nickname and from Nickname to Person_Nickname. This allows a given person to have as many nicknames as you like.
Then, to find all persons which match a given nickname, you can write:
您需要 2 个表:
在表“用户”中,您有用户的 ID、姓名和其他可选信息。
在“昵称”表中,您有 user_id、昵称。
这是一对多关联。
然后,要获取所有用户昵称的列表,您可以像这样查询(使用用户 ID):
作为一个不太像数据库的实现,您可以这样做:
仅使用一张包含 2 个字段(用户、昵称)的表。
然后,您将获得与具有如下查询的用户关联的所有 john 昵称(您也可以使用 ids)的列表:
CSV 方法有效,但您需要实现自己的函数来添加/删除/解析昵称,并且它几乎肯定会比我解释的两个实现慢。
You need 2 tables:
In table "users" you have id, name, other optional information on the user.
In table "nicknames" you have user_id, nickname.
It's a one to many assosiation.
Then to obtain a list of all user nicknames you query it like this (using the user id):
As a less database like implementation you could do this:
use only one table with 2 fields (user, nickname).
Then you get a list of all john's nicknames (you could use ids, too) associated with a user with a query like this:
The CSV approach works but you would need to implement your own function to add/remove/parse nicknames, and it would almost certainly be slower that the two implementations I explained.