SQLite - 对同一数据/列的多个条目进行选择

发布于 2024-08-11 23:56:18 字数 459 浏览 5 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分分钟 2024-08-18 23:56:18

对于初学者来说,这就是您所拥有的

SELECT Name, Nickname FROM MyTable WHERE Nickname = 'Copper';

,但我强烈建议使用一个名称表和一个使用的昵称表,以便昵称具有对名称的引用。

CREATE TABLE Users (
    UserId INTEGER PRIMARY KEY, 
    Name TEXT
    );
CREATE TABLE Nicknames (
    NickNameId INTEGER PRIMARY KEY, 
    UserId INTEGER REFERENCES Users(UserId), 
    NickName Text
    );

您会发现此方案将为您提供更多控制来编辑条目、删除条目等。

使用 INNER JOIN 进行查询:

SELECT Users.Name, NickNames.NickName 
    FROM Users INNER JOIN NickNames ON User.UserId=NickNames.UserId
    WHERE NickNames.NickName = 'Copper';

或嵌套查询:

SELECT Users.Name
    FROM Users
    WHERE User.UserId IN (
    SELECT NickNames.UserId 
        FROM NickNames 
        WHERE NickNames.NickName = 'Copper');

这两者相当于(在本例中)指定使用 WHERE 子句进行连接。它有效,但形式很差(它不像INNER JOIN那么清晰):

SELECT Users.Name, NickNames.NickName 
    FROM Users, NickNames 
    WHERE User.UserId = NickNames.UserId
    AND NickNames.NickName = 'Copper';

For starters, here is what you have

SELECT Name, Nickname FROM MyTable WHERE Nickname = 'Copper';

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.

CREATE TABLE Users (
    UserId INTEGER PRIMARY KEY, 
    Name TEXT
    );
CREATE TABLE Nicknames (
    NickNameId INTEGER PRIMARY KEY, 
    UserId INTEGER REFERENCES Users(UserId), 
    NickName Text
    );

You will find this scheme will give you more control to edit entries, remove them, etc.

Query it with either an INNER JOIN:

SELECT Users.Name, NickNames.NickName 
    FROM Users INNER JOIN NickNames ON User.UserId=NickNames.UserId
    WHERE NickNames.NickName = 'Copper';

Or a nested query:

SELECT Users.Name
    FROM Users
    WHERE User.UserId IN (
    SELECT NickNames.UserId 
        FROM NickNames 
        WHERE NickNames.NickName = 'Copper');

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 as INNER JOIN):

SELECT Users.Name, NickNames.NickName 
    FROM Users, NickNames 
    WHERE User.UserId = NickNames.UserId
    AND NickNames.NickName = 'Copper';
与酒说心事 2024-08-18 23:56:18

为什么不只使用下表:

Person(列:person_id,person_name)

Nickname(列:nickname_id,nickname)

Person_Nickname(列:person_id,nickname_id)

然后创建从 Person 到 Person_Nickname 以及从 Nickname 到 Person_Nickname 的外键。这使得某个人可以拥有任意多个昵称。

然后,要查找与给定昵称匹配的所有人员,您可以编写:

SELECT p.person_name
  FROM person.p
     , nickname n
     , person_nickname pn
 WHERE n.nickname = 'nickname of interest'
   AND p.person_id = pn.person_id 
   AND n.nickname_id = pn.nickname_id

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:

SELECT p.person_name
  FROM person.p
     , nickname n
     , person_nickname pn
 WHERE n.nickname = 'nickname of interest'
   AND p.person_id = pn.person_id 
   AND n.nickname_id = pn.nickname_id
姐不稀罕 2024-08-18 23:56:18

您需要 2 个表:

  • 用户
  • 昵称

在表“用户”中,您有用户的 ID、姓名和其他可选信息。
在“昵称”表中,您有 user_id、昵称。

这是一对多关联。
然后,要获取所有用户昵称的列表,您可以像这样查询(使用用户 ID):

SELECT nicknames.nickname FROM nicknames WHERE nicknames.user_id=id

作为一个不太像数据库的实现,您可以这样做:
仅使用一张包含 2 个字段(用户、昵称)的表。
然后,您将获得与具有如下查询的用户关联的所有 john 昵称(您也可以使用 ids)的列表:

SELECT table.nickname FROM table WHERE table.user="john"

CSV 方法有效,但您需要实现自己的函数来添加/删除/解析昵称,并且它几乎肯定会比我解释的两个实现慢。

You need 2 tables:

  • users
  • nicknames

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):

SELECT nicknames.nickname FROM nicknames WHERE nicknames.user_id=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:

SELECT table.nickname FROM table WHERE table.user="john"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文