-2

Lets say I have 2 tables, A and B, and they look something like this:

Table A - Orders
+----+-------------+------------+
| ID | Description | Product ID |
+----+-------------+------------+
|  1 | ABC         |          4 |
|  2 | DEF         |          5 |
|  3 | GHI         |          6 |
+----+-------------+------------+

Table B -  Products
+----+-------------+
| ID | Name        |
+----+-------------+
|  4 | cat         |
|  5 | cactus      | 
|  6 | cow         | 
+----+-------------+

I want to return results of rows in table A if the "Name" column in table B matches the search term, using something like LIKE %searchterm% in SQL, if applicable. The basic idea is that based on the product ID in table A it would then look for the row in table B with that ID and then check if the name column matches the search term, and if so return the row from table A with the related product ID.

So the search term was "ca" and the query was run, it would return rows 1 and 2 (cat and cactus) from table A since the products in table B match the search term.

Dharman
  • 30,962
  • 25
  • 85
  • 135
ItzJustJosh
  • 34
  • 2
  • 7
  • 2
    Do you know about `JOIN`s? If not it's easy to look it up – ADyson Oct 04 '19 at 17:37
  • What have you tried so far??? This is just a very simple `JOIN`. At least attempt something before posting a question. – Eric Oct 04 '19 at 17:43
  • I will research JOIN. I haven't tried anything yet since I was stumped as to how to go about creating this query. – ItzJustJosh Oct 04 '19 at 18:04

1 Answers1

0

I think you should use subquery

SELECT * FROM A WHERE Product_Id IN (SELECT ID FROM B WHERE Name LIKE '%ca%') 
Rez
  • 514
  • 1
  • 9
  • 31