Search Query

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
jamrop
Forum Commoner
Posts: 80
Joined: Fri May 16, 2003 5:38 pm

Search Query

Post by jamrop »

Hey

Is it possible to search from 2 tables in mysql?

e.g. i have an advert table and an announement table.
I want to try and make a search form , that will query the database of keywords and search the 2 tables

is that possible?

i know that one table would be

select * from advert where description like "%blah%";

but not sure if you can do 2 tables. They do not have a relationship.
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

as long as the tables live in the same database you can use a join, otherwise use two mysql connections.
Here is a straight two table example, be sure to check a common key within your 'WHERE' statement to avoid duplicate rows.

Code: Select all

$query="SELECT DISTINCT stories.story_id, stories.type, stories.publish, library.story_ID, library.headline, library.byline, library.entrydate,
FROM stories, library 
WHERE stories.story_id= library.story_ID 
&& stories.publish ='Y' 
ORDER by entrydate DESC LIMIT 10";
$result=mysql_query($query) or die(mysql_error());
go here for more:
http://www.mysql.com/doc/en/JOIN.html
Post Reply