0

I hoping someone can help me with a MySQL query.

I am trying to create a query in a MySQL database that finds all rows that matches a string 'foo' in column_1 of Table_A and then searches those rows with 'foo' and for all values of 'bar' in column_2 of Table_A returns all rows in Table_b that contain 'bar' in column_8.

My starting query is:

SELECT * FROM TABLE_A WHERE column_1 LIKE 'foo';

The above query only finds all data that matches a string 'foo' in column_1 of Table_A. What code do I need to add to take the above results and use Table_A column_2 to search for matches in column_8 of Table_B? I can't seem to figure out how to perform this query.

Thanks in advance for any help you can provide.

1
  • Your request doesn't make any sense. Is there a relationship between Table_A and Table_B? As explained you don't even need Table_A Commented Jul 15, 2011 at 18:32

4 Answers 4

3

Its not clear from your description if this is right but it sounds like you're looking for a JOIN

SELECT * 
FROM 
    TABLE_A a
  INNER JOIN TABLE_B b 
  ON a.column_2 = b.column_8
 WHERE 
    a.column_1 LIKE 'foo'
     and a.column_2 = 'bar'
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Seriously, thanks for taking the time to read through my complex description. You code worked perfectly!
1

you can use a sub query to select the values in table b to compare to the column in table A:

select * from tableA
Where column1 = 'foo'
and column2 in (select disitnct column8 from tableB where xxxxxxxxx)

1 Comment

You can format your code by selecting and pressing the {} button.
0

http://dev.mysql.com/doc/refman/5.0/en/join.html

For example:

SELECT * FROM TABLE_A 
LEFT JOIN TABLE_B 
USING(COLUMN TO MATCH ON)
WHERE TABLE_A.column_1 LIKE 'foo' 
AND  TABLE_B.column_8 LIKE 'bar';

I'm not entirely clear what you are actually trying to do so that's probably not the exact code you need.

1 Comment

Thanks! I completely forgot about that :)
0

SELECT * FROM TABLE_A INNER JOIN TABLE_B ON TABLE_A.COLUMN_1 = 'foo' AND TABLE_A.COLUMN_2 = 'bar' AND TABLE_B.COLUMN_8 = 'bar'

This would work on SQL server, probably is the same on MySQL. Try it! You can use more than one criterion on a join.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.