简体   繁体   English

MySQL查询:当另一列中的值符合特定条件时,返回一列中具有特定值的所有行

[英]MySQL Query: Return all rows with a certain value in one column when value in another column matches specific criteria

This may be a little difficult to answer given that I'm still learning to write queries and I'm not able to view the database at the moment, but I'll give it a shot. 考虑到我仍在学习编写查询并且目前无法查看数据库,这可能有点难以回答,但是我会试一试。

The database I'm trying to acquire information from contains a large table (TransactionLineItems) that essentially functions as a store transaction log. 我试图从中获取信息的数据库包含一个大表(TransactionLineItems),该表实际上起着商店事务日志的作用。 This table currently contains about 5 million rows and several columns describing products which are included in each transaction (TLI_ReceiptAlias, TLI_ScanCode, TLI_Quantity and TLI_UnitPrice). 该表当前包含约500万行和几列,描述了每笔交易中包含的产品(TLI_ReceiptAlias,TLI_ScanCode,TLI_Quantity和TLI_UnitPrice)。 This table has a foreign key which is paired with a primary key in another table (Transactions), and this table contains transaction numbers (TRN_ReceiptNumber). 该表具有一个外键,该外键与另一个表(事务)中的主键配对,并且该表包含事务编号(TRN_ReceiptNumber)。 When I join these two tables, the query returns one row for every item we've ever sold, and each row has a receipt number. 当我连接这两个表时,查询将为我们售出的每件商品返回一行,并且每一行都有一个收据编号。 16 rows might have the same receipt number, meaning that all of these items were sold in a single transaction. 16行可能具有相同的收据编号,这意味着所有这些项目都是在一次交易中出售的。 Below that might be 12 more rows, each sharing another receipt number. 在此之下可能还有12行,每行共享另一个收据编号。 All transactions are broken down into multiple rows like this. 像这样将所有事务分解成多行。

I'm attempting to build a query which returns all rows sharing a single receipt number where at least one row with that receipt number meets certain criteria in another column. 我正在尝试建立一个查询,该查询返回所有共享一个收据号的行,其中至少一个具有该收据号的行满足另一列中的某些条件。 For example, three separate types of gift cards all have values in the TLI_ScanCode column that begin with "740000." 例如,三种独立类型的礼品卡在TLI_ScanCode列中均具有以“ 740000”开头的值。 I want the query to return rows with values beginning with these six digits in the TLI_ScanCode column, but I would also like to return all rows which share a receipt number with any of the rows which meet the given scan code criteria. 我希望查询返回的行的值在TLI_ScanCode列中以这六位数字开头,但是我还想返回所有与给定扫描代码条件的行共享收据号的所有行。 Essentially, I need the query to return all rows for every receipt number which is also paired in at least one row with a gift card-related scan code. 本质上,我需要查询以返回每个收据编号的所有行,该收据编号也至少与礼品卡相关的扫描代码配对。

I attempted to use a subquery to return a column of all receipt numbers paired with gift card scan codes, using "WHERE A.TRN_ReceiptAlias IN ( subquery ..." to return only those rows with a receipt number which matched one of the receipt numbers returned by the subquery. This appeared to run without issue for five minutes before the server ground to a halt for another twenty while it processed the query. The query appeared to complete successfully, but given that I was working with IT to restore normal store operations during this time I failed to obtain the results of the query (apart from the associated shame and embarrassment). 我尝试使用子查询返回所有收据编号与礼品卡扫描码配对的列,并使用“ WHERE A.TRN_ReceiptAlias IN( 子查询 ...”)仅返回那些收据编号与其中一个收据编号匹配的行由子查询返回。这似乎没有问题运行了五分钟,然后服务器在处理查询时又停了二十分钟,查询似乎成功完成了,但是鉴于我正在与IT一起恢复正常的商店运作在这段时间里,我没有获得查询的结果(除了相关的耻辱和尴尬)。

I'd like to know if there is a way to write a query to obtain this information without causing the server to hang. 我想知道是否有一种方法编写查询以获取此信息而不会导致服务器挂起。 I'm assuming that either: a) it wasn't very smart to use a subquery in this manner on such a large table, or b) I don't know enough about SQL to obtain the information I need. 我假设是:a)在这么大的表上以这种方式使用子查询并不是很聪明,或者b)我对SQL不够了解,无法获得所需的信息。 I'm assuming the answer is both A and B, but I'd very much like to learn how to do this the right way. 我假设答案是A和B,但是我非常想学习如何正确地做到这一点。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

SELECT *
  FROM a as a1
  JOIN b
    ON b.id = a.id
  JOIN a as a2
    ON a2.id = b.id
 WHERE b.some_criteria = 'something';

Include an index on (b.id,b.some_criteria) 在(b.id,b.some_criteria)上添加索引

You aren't the first person, nor will you be the last to bring down your system with an inefficient query. 您不是第一个人,也不是最后一个以低效率查询降低系统性能的人。

The most important lesson is that "Decision Support" and "Analytics" really don't co-exist with a transaction system. 最重要的一课是,“决策支持”和“分析”确实不与交易系统共存。 You really want to pull the data into a datamart or datawarehouse or some other database that isn't your transaction database, so that you don't take the business offline. 您确实希望将数据拉入数据集市,数据仓库或不是事务数据库的其他数据库中,以免业务脱机。

In terms of understanding why your initial query was so inefficient, you want to familiarize yourself with the EXPLAIN EXTENDED syntax that returns you plan information that should help you debug your query and work on making it perform acceptably. 为了理解为什么初始查询如此低效,您需要熟悉EXPLAIN EXTENDED语法,该语法将返回您计划的信息,这些信息将帮助您调试查询并努力使其能够令人满意地执行。 If you update your question with the actual explain plan output for it, that would be helpful in determining what the issue is. 如果使用实际的解释计划输出来更新问题,这将有助于确定问题所在。

Just from the outline you provided, it does sound like a self join would make sense rather than the subquery. 仅从您提供的大纲来看,听起来像是自我联接而不是子查询很有意义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mysql将具有相同列值的两行连接起来,并计算某些列值,并在一行中返回所有行 - mysql join two rows with same column value and calculate certain column values and return all rows in one row mySQL 连接表,但根据来自另一列的匹配条件排除具有特定 ID/值的所有行(条件仅包含在某些行中) - mySQL Join Tables but exclude ALL rows with a certain ID/value based on matching criteria from another column (criteria contained in only some rows) 过滤所有行的特定条件,并为具有相同值的每一列仅返回一行 - Filtering down specific criteria for all rows and return only one row for each column with the same value MySQL:全选,除了当一列包含一个特定值而另一列包含另一特定值时 - MySQL: SELECT all except when one column contains one particular value and another contains another specific value MySQL 查询一列值相同而另一列值不同的行 - MySQL query rows with same value in one column and different value in another MySQL返回在另一列中存在一个值的行 - MySQL return rows where one value exists in another column MySQL-只有一列中的值的行与另一列中的所有行匹配 - Mysql - Only rows that value from one column matches for all from other column 如何获取列值与MySql中选择查询的行匹配的表的所有行 - How to get all the rows of a table who's column value matches the rows of a select query in MySql Mysql SELECT 并返回多行,但在存在时更喜欢一个列值 - Mysql SELECT and return multiple rows, but prefer one column value over another when present MySQL-在列中最后一次出现特定值之后,选择所有符合条件的行 - MySQL - Select all rows matching criteria after the last occurrence of a specific value in a column
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM