Jump to content

Recommended Posts

I created a search function for a website that I am working on. Right now it is only searching one table. I would like to make it so it searches two tables.

 

I am trying to do this the simplest way possible. So as not to clutter up my code anymore than I have two. The way that I have been trying to get it to do it is

Select * from ttmautos, inperson_autos Where inperson_autos.l_name = 'something' or ttmautos.l_name = 'something'

 

When I use this code I get repeated rows from the table. When I was searching online to see if I could figure out what the problem was and most things I read always used AND.

 

The information does not need to be in both tables. When they are using the search feature of the site. The information could be in either table or in could pull from both tables.

 

I have read about Joins. I am not sure if I am to new to MySql to understand them. I have also read about unions and what I understand is that both tables have to have the same number of columns and same type of field data. Both of my tables do not have the same number of columns.

 

I would appreciate any help on this issue.

Link to comment
https://forums.phpfreaks.com/topic/254385-searching-multiple-tables/
Share on other sites

Hi

 

You current SQL will do a cross join, producing a row for every combination of rows on the 2 tables and then bring back any of those combination rows where either name is 'something'.

 

So if you have 100 rows on each table with only 2 rows on one table where the name matches it will bring back 200 rows.

 

All the best

 

Keith

I didn't see any schemas posted.

 

Yes, that's true.

 

You need 2 separate queries.

 

Actually, I believe only the return result from table A and table B need to be the same, not the actual tables.

 

So:

 

(select animal_id as id, animal_name as name from animal_table)
UNION
(select person_id as id, person_name as name from person_table)

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.