title: MySQL Query: Return all rows with a certain value in one column when value in another column matches specific criteria
body: <p>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.</p>
  
  <p>The database I'm trying to acquire information from contains a large table (TransactionLineItems) that essentially functions as a store transaction log.  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).  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).  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.  Below that might be 12 more rows, each sharing another receipt number.  All transactions are broken down into multiple rows like this.</p>
  
  <p>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."  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.  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.</p>
  
  <p>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 (<em>subquery</em>..." 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).</p>
  
  <p>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.  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.  Any help would be greatly appreciated.  Thanks!</p>