Mortgage Calculator - MPAA - Best Credit Cards - Polish classifieds in the USA - Forex news

PDA

View Full Version : selecting a field from MULTIPLE tables


Weirfire
Oct 6th 2005, 4:18 am
I have a number of different tables all containing a field called keywords

I want to list all the titles from each table where a certain keyword exists and I was wondering if it is possible to do this in a single query?

Would something like

SELECT title FROM table1, table2, table3 WHERE keywords LIKE '%$keyword%'


work?

DangerMouse
Oct 6th 2005, 4:23 am
you can do!

select table1.field as field1, table2.field as field2
from table1
inner join table2 on table1.fieldx = table2.fieldy
where ...

I don't think you can actually merge 2 fields together though...

dct
Oct 6th 2005, 4:24 am
Are the tables connect in anyway?

If not (and even if so) you'll probably be best using union

SELECT title FROM table1 WHERE keywords LIKE '%$keyword%'
UNION
SELECT title FROM table2 WHERE keywords LIKE '%$keyword%'
UNION
SELECT title FROM table3 WHERE keywords LIKE '%$keyword%'

Weirfire
Oct 6th 2005, 4:42 am
Thanks guys. Thats what I needed to know! :)