[英]How do I select a record from one table in a mySQL database, based on the existence of data in a second?
Please forgive my ignorance here. 请原谅我的无知。 SQL is decidedly one of the biggest "gaps" in my education that I'm working on correcting, come October.
SQL无疑是我正在努力进行纠正的教育方面最大的“空白”之一,将于10月发布。 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
. 一个是
users
,另一个是conversation_log
。 The basic structure is outlined below: 基本结构概述如下:
users: 用户:
conversation_log conversation_log
(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. 我想做的是从users表中返回一个名称列表,该名称列表在session_log表中至少有一条记录。 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.
目前,我正在使用两个单独的SQL语句来执行此操作,其中一个检查session_log中的记录的调用被称为数百次(如果不是数千次),对于每个用户ID一次,只是为了查看该ID是否存在记录。
Currently, the two SQL statements are as follows: 当前,这两个SQL语句如下:
select id
from users
where 1; 从1个
users
中选择id
; (gets the list of userid values for the next query) (获取下一个查询的用户ID值列表)
select id
from conversation_log
where userid
= $userId limit 1; 从
conversation_log
userid
选择id
,其中userid
= $ userId限制1; (checks for existing records) (检查现有记录)
Right now I have 4,000+ users listed in the users table. 现在,我在用户表中列出了4,000多个用户。 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.
任何帮助将不胜感激。
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. 现在,JOIN本身就是一个“内部联接”,这意味着它将仅返回两个表共有的行。 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.
换句话说,如果特定的session_log.userid不存在,则不会为该用户ID返回行,用户或会话日志的任何部分。
Also, +1 for having a clearly worded question : ) 另外,如果您有明确的问题,请+1 :)
EDIT: I added a "DISTINCT", which means to filter out all of the duplicates. 编辑:我添加了一个“ DISTINCT”,这意味着要筛选出所有重复项。 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.
如果用户出现在一个以上的session_log行中,并且您没有DISTINCT,则将多次获得该用户的名称。 This is because JOIN does a cartesian product , or does every possible combination of rows from each table that match your JOIN ON criteria.
这是因为JOIN执行笛卡尔积 ,或执行每个表中符合JOIN ON准则的行的所有可能组合。
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
. 用简单的英语:选择每一行
users
,使得存在至少一个排conversation_log
与匹配的userid
。
What you need to read is JOIN syntax. 您需要阅读的是JOIN语法。
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 如果您希望HAVING count(*)> 0,则可以在末尾添加
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.