Select from 2 different tables

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
easinewe
Forum Newbie
Posts: 23
Joined: Mon Jan 11, 2010 3:22 pm

Select from 2 different tables

Post by easinewe »

Hello,
I am creating a search function and would like this two work with 2 or more tables; Dog Catalog and portfolio in this example. What am I doing wrong here?

"SELECT * FROM portfolio, Dog Catalog WHERE
Dog Catalog.title LIKE '%$find%' or
portfolio.client LIKE '%$find%'";

I get errors whenever I try to echo a result.

Any help would be appreciated.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: Select from 2 different tables

Post by aravona »

you need to use a join: http://www.w3schools.com/SQl/sql_join.asp

Code: Select all

"SELECT * FROM portfolio, Dog Catalog WHERE 
Dog Catalog.title LIKE '%$find%' or 
portfolio.client LIKE '%$find%'";

Code: Select all

SELECT *
FROM portfolio
INNER JOIN Dog Catalog
ON portfolio.column_name = Dog Catalog.column_name (needs to be the PK and FK)
WHERE Dog Catalog.title LIKE '%$find%' or 
portfolio.client LIKE '%$find%'
That should work with your keys in. But read up on joins first so you have more of an idea at whats going on :)
Post Reply