Jump to content

search site (3+ tables)


phorcon3

Recommended Posts

I wanna do a search on three or more tables at once, but how would I do this?

 

here's a mysql sample:

 

CREATE TABLE `table1` (
`id` int(11) NOT NULL auto_increment,
`col1` varchar(255) NOT NULL default '',
`col2` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1;

CREATE TABLE `table2` (
`id` int(11) NOT NULL auto_increment,
`col1` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1;

CREATE TABLE `table3` (
`id` int(11) NOT NULL auto_increment,
`col1` varchar(255) NOT NULL default '',
`col2` varchar(255) NOT NULL default '',
`col3` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1;

 

so, what I need to do is:

 

a) find the total number of results, so I suppose I will have to use Count(*) as Count somewhere?

 

and

 

b) find out from which table I got the current results

 

php script:

 

<?php

$search = addslashes($_GET['search']);

$a = mysql_query("");//query -> ex: SELECT on.col1, on.col2, tw.col1, tr.col1, tr.col2, tr.col3 FROM table1 as on, table1 as tw, table3 as tr WHERE on.col1 LIKE '%$search%' OR on.col2 LIKE '%$search%' etc. ORDER BY id DESC LIMIT 0,10
while($b = mysql_fetch_assoc($a))
{
//find out from which table!

echo 'this result is from table => '.$tablename;
}

echo 'the total search result is: '.$total;

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.