tags:

views:

141

answers:

1

I have 3 difrrent table structures, I will call them A,B,C.

I need to do a search on all those 3 tables at the same time, let's that I need to:

A.name LIKE %Google% B.company LIKE%Google% and C.company LIKE%Google%

It's posibile to get a query out for the "query type" mentionated?

Best Regards!

Here is what i'm looking for

Select from table A where A.name LIKE %something% inner join B on b.company LIKE %something% and after that I need to do the samething for table C..but I don't know how...

The query:

 SELECT name FROM firmen WHERE name LIKE '%Felsengartenkellerei Besigheim eG%'
  UNION
   SELECT firma FROM daten WHERE firma LIKE '%Felsengartenkellerei Besigheim eG%'
UNION
  SELECT firma FROM ver WHERE firma LIKE '%Felsengartenkellerei Besigheim eG%'
+1  A: 

Not sure exactly what you're trying to do, since we don't have the schema, but presumably A is a table of companies, where the 'name' field corresponds to a foreign-key 'company' field in both B and C, if that is so then you'd some thing like:

SELECT * FROM A, B, C
  WHERE A.name LIKE '%Google%' 
  AND B.company = A.name
  AND C.company = A.name

Edit, so from your comment in the question where you say they're not foreign keys, you want to select all rows that have a particular filed 'LIKE' Google, you just do them one at a time:

SELECT * FROM A WHERE name    LIKE '%Google%'
SELECT * FROM B WHERE company LIKE '%Google%'
SELECT * FROM B WHERE company LIKE '%Google%'

If all 3 of these tables had the same schema (or they have a few fields in common and you just select those in a corresponding order), you could take the UNION of these 3 queries if you wanted to join the results together.

Suppose all 3 of them have a 'url' or 'webaddr' field, and they have 'desc', 'description', and 'info' fields respectively:

SELECT url, desc            FROM A WHERE name    LIKE '%Google%'
UNION
SELECT webaddr, description FROM B WHERE company LIKE '%Google%'
UNION
SELECT url, info            FROM B WHERE company LIKE '%Google%'

There's a nice explanation of the semantics of UNION and UNION ALL in MySQL on tutorialspoint

David Claridge
Can you please give me an example with UNION how to join the results together please, I haven't slept for about 50h so I can't think to much now.Thank you
Uffo
updated to give an example of UNION
David Claridge
My query looks like this:SELECT name FROM firmen WHERE name LIKE %Felsengartenkellerei Besigheim eG%UNIONSELECT company FROM daten WHERE company LIKE %Felsengartenkellerei Besigheim eG%UNIONSELECT company FROM ver WHERE company LIKE %Felsengartenkellerei Besigheim eG%An the error:--------------------------#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%Felsengartenkellerei Besigheim eG%UNIONSELECT company FROM daten WHERE compan' at line 1 -----------------------
Uffo
Yeah..I guess it cannot be done!
Uffo
you need quotes around your LIKE string, probably better if you put your full query in your original post, indented so we get highlighting
David Claridge