简体   繁体   English

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

[英]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: 用户:

  • id (INT) ID(INT)
  • name (TXT) 名称(TXT)

conversation_log conversation_log

  • userid (INT) // same value as id in users - actually the only field in this table I want to check userid(INT)//与用户ID相同的值-实际上是我要检查的此表中的唯一字段
  • input (TXT) 输入(TXT)
  • response (TXT) 回应(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. 我想做的是从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.

相关问题 如何在分组时基于一个字段中最长的“字符串”从mysql数据库中选择记录? - How do I select record from mysql database based on the longest 'string' in one filed when grouping? 如何在MySQL中使用SELECT语句从一个表中获取数据,并从另一表中覆盖数据? - How do I take data from one table, and overwrite it from another table, with a SELECT statement in MySQL? 我如何总是从一个表中提取数据,又如何从第二个表中提取数据(如果MySQL中存在的话)? - How do I always pull data from one table, but also pull data from a second table if it's there in MySQL? 如何使用php和mysql在一个表中搜索记录是否存在 - How to search for existence of record in one table from another using php and mysql 如何根据某些列选择mysql记录并在表中多次显示? - How do i select mysql record and display in table multiple times based on certain columns? 如何从MySQL表中选择一行? - How do I select one row from a MySQL table? MYSQL仅在该表的列值与另一表的列值匹配的情况下,才如何从该表中选择数据? - MYSQL How do I select data from one table only where column values from that table match the column values of another table? 如何有效地从2个mysql表中选择唯一的行/记录数据并返回一个按时间戳排序的合并结果? - How do I efficiently select unique row/record data from 2 mysql tables and return one merged result, ordered by timestamp? 如何从一个表中返回一行,其中包含MySQL中第二个表中匹配id的数量? - How do I return a row from one table with the number of matching id's from a second table in MySQL? 如何从下拉列表中选择mysql数据库表以插入记录 - How to select mysql database table from a dropdown list to insert record
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM