We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,280 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Search query in 2 tables

Hi,

I'm making a small web-based software and I need help for a search query that looks for results in 2 tables.

I can run it very well like this for a single table :

$query=mysql_query("SELECT * FROM `customers` WHERE `name` LIKE \"%$search%\" ORDER BY `name` ASC");

But, here is the tricky part. I have 2 tables :

Table 1 (customers) :
client_id : 8
name : Johnny Depp
address : bla bla bla
etc...

Table 2 (animals) :
animal_id : 52
client_id : 8 (it refers to the client)
Name : Rex
etc....

So, Johnny Depp has a Dog named Rex. I have a search field, if I type "Johnny Depp", or "Depp", it will give me "Johnny Depp (client_id:8)" as the result, but I want to be also able to get the same result with the name of his dog. If I type "Rex", the result will be "Johnny Depp (client_id:8)", because in table 2, there is a dog named Rex with the client_id 8.

How can I do that? Thank you very much for your help !

5
Contributors
7
Replies
1 Day
Discussion Span
2 Months Ago
Last Updated
39
Views
JeanPhilippe
Newbie Poster
14 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

try this

"select a.client_id,a.name,a.address,b.animal_id,b.name from customers a, animal b
where a.client_id=b.client_id and a.name like '% John%'

here we join two tables customers with name a and animal with name b
and search like from a table . But you can search from any field of any table but use refrence a, or b

Regards,
Shahid Qayyum
Pakistan

shahidpk
Newbie Poster
3 posts since Oct 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

As @shahidpk has said, use a join but they have missed the search on the animlas table

$query=mysql_query("SELECT c.*, a.* FROM customers c INNER JOIN animals a WHERE c.name LIKE '".%$search%."' OR a.name LIKE '".%$search%."' ORDER BY c.name ASC");
simplypixie
Practically a Master Poster
642 posts since Oct 2010
Reputation Points: 157
Solved Threads: 118
Skill Endorsements: 5

Joins are good but i'm trying to use separate selects, it is faster sometimes especially for big tables.

I'll show you the simple MySQL version.

$sql = mysql_query("SELECT * FROM table1 WHERE id = '3'");

if(mysql_num_rows($sql) > 0) {
    $arr = mysql_fetch_array($sql);

    // you also can use different loops

    $sql = mysql_query("SELECT * FROM table2 WHERE id = '".$arr['id']."');
}
szabizs
Junior Poster
128 posts since Jul 2009
Reputation Points: 8
Solved Threads: 15
Skill Endorsements: 0

Thanks for all your responses. The code from @simplypixie works :) But I had to invert the table name. It works when I search for the animal name, it gives me the client, but when I search for the client, it gives me the same result multiple times (the number of animals he has)...How can we fix that?

Thanks.

JeanPhilippe
Newbie Poster
14 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Not sure what you mean by invert the table name?? What is your query now please.

simplypixie
Practically a Master Poster
642 posts since Oct 2010
Reputation Points: 157
Solved Threads: 118
Skill Endorsements: 5

$query=mysql_query("SELECT * FROM customer WHERE name LIKE '%.$customer_name.%' OR id=(select id from animals where name LIKE '%.$pet_name.%')");

engrmshahzad
Newbie Poster
3 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

OK, I think I understand a bit more now (plus I missed off the ON .... from my first query) - you want to return the name of the customer if the search matches their name or their animal's name. If I am correct then your query needs to be

$query=mysql_query("SELECT c.* FROM customers c INNER JOIN animals a ON c.id=a.id WHERE c.name LIKE '".%$search%."' OR a.name LIKE '".%$search%."' ORDER BY c.name ASC");
simplypixie
Practically a Master Poster
642 posts since Oct 2010
Reputation Points: 157
Solved Threads: 118
Skill Endorsements: 5

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0811 seconds using 2.72MB