如何根据一秒内数据的存在情况从 mySQL 数据库的一个表中选择一条记录?
在此请原谅我的无知。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须执行所谓的“加入”。这,嗯,根据两个表的共同值将它们连接在一起。
看看这对您是否有意义:
现在 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:
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.
像这样:
用简单的英语来说:选择
users
中的每一行,这样conversation_log
中至少有一行具有匹配的userid
。Something like this:
In plain English: select every row from
users
, such that there is at least one row fromconversation_log
with the matchinguserid
.您需要阅读的是 JOIN 语法。
如果你愿意的话可以在最后添加
有计数(*)> 0
What you need to read is JOIN syntax.
You could add at the end if you wanted
HAVING count(*) > 0