如何根据一秒内数据的存在情况从 mySQL 数据库的一个表中选择一条记录?

发布于 2024-12-05 23:56:45 字数 923 浏览 0 评论 0原文

在此请原谅我的无知。 SQL 无疑是我所受教育中最大的“差距”之一,我正在努力纠正,到 10 月份。场景如下:

我在数据库中有两个表,我需要从中访问某些数据。一个是users,另一个是conversation_log。基本结构概述如下:

users:

  • id (INT)
  • name (TXT)

conversation_log

  • userid (INT) // 与 users 中的 id 值相同 - 实际上是 users 中的唯一字段这张表我想检查
  • 输入(TXT)
  • 响应(TXT)

(请注意,我只列出与当前挑战相关的字段的结构)

我想要做的是返回一个列表用户表中的名称至少有conversation_log 表中的一条记录。目前,我使用两个单独的 SQL 语句来执行此操作,其中检查对话日志中的记录的语句被调用数百次(如果不是数千次),每个用户 ID 一次,只是为了查看该 ID 是否存在记录。

目前两条SQL如下:

select id from users where 1; (获取下一个查询的 userid 值列表)

select id from conversation_log where userid = $userId limit 1; (检查现有记录)

现在,用户表中列出了 4,000 多个用户。我相信你可以想象这个方法需要多长时间。我知道有一种更简单、更有效的方法可以做到这一点,但由于自学,这是我尚未学习的东西。任何帮助将不胜感激。

Please forgive my ignorance here. SQL is decidedly one of the biggest "gaps" in my education that I'm working on correcting, come October. Here's the scenario:

I have two tables in a DB that I need to access certain data from. One is users, and the other is conversation_log. The basic structure is outlined below:

users:

  • id (INT)
  • name (TXT)

conversation_log

  • userid (INT) // same value as id in users - actually the only field in this table I want to check
  • input (TXT)
  • response (TXT)

(note that I'm only listing the structure for the fields that are {or could be} relevant to the current challenge)

What I want to do is return a list of names from the users table that have at least one record in the conversation_log table. Currently, I'm doing this with two separate SQL statements, with the one that checks for records in conversation_log being called hundreds, if not thousands of times, once for each userid, just to see if records exist for that id.

Currently, the two SQL statements are as follows:

select id from users where 1; (gets the list of userid values for the next query)

select id from conversation_log where userid = $userId limit 1; (checks for existing records)

Right now I have 4,000+ users listed in the users table. I'm sure that you can imagine just how long this method takes. I know there's an easier, more efficient way to do this, but being self-taught, this is something that I have yet to learn. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

荆棘i 2024-12-12 23:56:45

您必须执行所谓的“加入”。这,嗯,根据两个表的共同值将它们连接在一起。

看看这对您是否有意义:

SELECT DISTINCT users.name
FROM users JOIN conversation_log ON users.id = converation_log.userid

现在 JOIN 本身就是一个“内部联接”,这意味着它只会返回两个表共有的行。换句话说,如果特定的 conversation_log.userid 不存在,则不会返回该用户 ID 的行、用户或对话日志的任何部分。

另外,+1 表示有一个措辞清晰的问题:)

编辑:我添加了一个“DISTINCT”,这意味着过滤掉所有重复项。如果某个用户出现在多个对话日志行中,并且您没有 DISTINCT,则您将多次获取该用户的名称。这是因为 JOIN 执行 笛卡尔积,或者执行每个表中匹配的行的所有可能组合您的加入标准。

You have to do what is called a 'Join'. This, um, joins the rows of two tables together based on values they have in common.

See if this makes sense to you:

SELECT DISTINCT users.name
FROM users JOIN conversation_log ON users.id = converation_log.userid

Now JOIN by itself is an "inner join", which means that it will only return rows that both tables have in common. In other words, if a specific conversation_log.userid doesn't exist, it won't return any part of the row, user or conversation log, for that userid.

Also, +1 for having a clearly worded question : )

EDIT: I added a "DISTINCT", which means to filter out all of the duplicates. If a user appeared in more than one conversation_log row, and you didn't have DISTINCT, you would get the user's name more than once. This is because JOIN does a cartesian product, or does every possible combination of rows from each table that match your JOIN ON criteria.

阿楠 2024-12-12 23:56:45

像这样:

SELECT *
FROM users
WHERE EXISTS (
    SELECT *
    FROM conversation_log
    WHERE users.id = conversation_log.userid
)

用简单的英语来说:选择users中的每一行,这样conversation_log中至少有一行具有匹配的userid

Something like this:

SELECT *
FROM users
WHERE EXISTS (
    SELECT *
    FROM conversation_log
    WHERE users.id = conversation_log.userid
)

In plain English: select every row from users, such that there is at least one row from conversation_log with the matching userid.

寄与心 2024-12-12 23:56:45

您需要阅读的是 JOIN 语法。

SELECT count(*), users.name 
FROM users  left join conversion_log  on users.id = conversation_log.userid
Group by users.name

如果你愿意的话可以在最后添加
有计数(*)> 0

What you need to read is JOIN syntax.

SELECT count(*), users.name 
FROM users  left join conversion_log  on users.id = conversation_log.userid
Group by users.name

You could add at the end if you wanted
HAVING count(*) > 0

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