Jump to content

Recommended Posts

Hi, I wonder if anyone can help me, I am building a search form on my site and it currently searches one table and displays the results from that row of the table, what I am trying to get it to do now is search two tables using one query so it will display the results pulled out from table one followed by the results from table two. I think I need to use the JOIN function but am having problems building the query. This is the original code that searches one table:

 

$query = "SELECT * FROM table1 WHERE field1 LIKE \"%$trimmed%\"";

 

This is how I have been trying to apply the code to search two tables with no luck so far:

 

$query = "SELECT * FROM table1, table2 WHERE table1.field1 LIKE \"%$trimmed%\" OR table2.field1  LIKE \"%$trimmed%\"";

 

Can anyone help me to apply the coding I need to search two tables simultaneously. Any help would be very much appreciated..

Link to comment
https://forums.phpfreaks.com/topic/225755-searching-2-mysql-tables-simultaneously/
Share on other sites

There are other ways besides this depending on how you want the results (not tested):

 

$query = "SELECT * FROM table1 WHERE field1 LIKE '%$trimmed%'
UNION SELECT * FROM table2 WHERE field1  LIKE '%$trimmed%'";

That code brings up no results and 'couldnt execute query'. The way I want the results displaying is two rows from each table, so sayfor example the first table has names and ages and the second table has names and gender and someone searches for john. The result would be:

 

john, 18

 

john, male

It sounds like you need something like this:

SELECT *
FROM table1 as t1
LEFT JOIN table2 as t2 
ON t1.field1 = t2.field1
WHERE t1.field1 LIKE \"%$trimmed%\";

 

This joins the two columns where the names are the same. It should return results like:

John, 18, male

 

all on one row. Also, because we are joining the two tables on field1, where they are equal, there is no use searching both columns... only searching one would be sufficient.

Thankyou for your response, the thing is that I was hoping to get the results to appear on separate lines rather than on the same line, is this possible?

 

You mean you want two ROWS right... not two lines?

 

You'll have to do something like AbraCadaver suggested using a UNION, or you could just concatenate two sql strings togther and use mysqli_multi_query() (which would be REALLY close to simultaneous).

 

If you don't mind me asking, why do you want two rows returned if you can have them both returned in one?

Say for example I had two tables, one for football players and one for staff, and then a user searches for 'Man Utd', I would want all the players for Man Utd with info about that player to be displayed one after the other followed by all the staff that worked for Man Utd with their info displayed. Does that make it any clearer?

There are other ways besides this depending on how you want the results (not tested):

 

$query = "SELECT * FROM table1 WHERE field1 LIKE '%$trimmed%'
UNION SELECT * FROM table2 WHERE field1  LIKE '%$trimmed%'";

 

This will do exactly what you want if the tables have the same number of columns.  If not then you need to specify the columns in each select and make sure you specify the same number of columns for each select:

 

SELECT field1, field2, field5 FROM table1 WHERE field8 LIKE '%$trimmed%'
UNION
SELECT field3, field4, field6 FROM table2 WHERE field2  LIKE '%$trimmed%'

 

If either doesn't work then you are doing something wrong.

Say for example I had two tables, one for football players and one for staff, and then a user searches for 'Man Utd', I would want all the players for Man Utd with info about that player to be displayed one after the other followed by all the staff that worked for Man Utd with their info displayed. Does that make it any clearer?

 

Ah, okay... I assume the table with staff has different columns then the one for the players, otherwise you would probably just combine the two tables and add another column for group ("staff" or "player") then order your results by group.

 

 

 

 

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.