Mysql select from 3 tables
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%'
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
精彩评论