Jump to content

Searching Multiple Tables


lpollard

Recommended Posts

I've tried many different queries and now I'm really stuck. I have 3 tables and will be adding more tables to search later on but I want to find out what the mysql query is, or what i have to do to search different columns from each table. Here are the three tables that I currently have:

 

Clients

| cl_id | tableid | cl_name | other fields...

 

Events

| ev_id | tableid | ev_name | other fields...

 

Userss

| usr_id | tableid | usr_first | usr_last | other fields...

 

Now when i want to display my search results I need to get

'usr_id' 'ev_id' 'cl_id' as one column;

'tableid' as one column;

'cl_name', ev_name', 'user_first & user_last' as one column.

 

If that doesn't make sense, or you need more info please let me know, thanks.

Link to comment
Share on other sites

I've just looked over trash and found one where I tried a union but failed at that too  :P

 

SELECT * FROM events UNION SELECT tableid, cl_name, cl_id FROM clients UNION SELECT tableid, usr_name, usr_id FROM users

WHERE events.ev_name like '%$trimmed%' OR clients.cl_name like '%$trimmed%' OR users.user_first like '%$trimmed%'

 

Link to comment
Share on other sites

Just tried and it searches the fields across the three tables, but is only returning the table id. And also it doesn't return the 3 tables together, it'll return a search result for one table only.

 

SELECT tableid, ev_name, ev_id FROM events WHERE ev_name LIKE '%$trimmed%'  UNION
SELECT tableid, usr_first, usr_id FROM users WHERE usr_first LIKE '%$trimmed%' UNION
SELECT tableid, cl_name, cl_id FROM clients WHERE cl_name LIKE '%$trimmed%'
LIMIT $start, $limit

Link to comment
Share on other sites

I think 'all'. But i'm not sure what you mean by which table to win.  :confused:

I appologise for beeing a noob on this matter, but i'll try and explain what i'm trying to do a bit better.


I have 3 mysql tables with different data that needs to be searched into 1 results table.

 

events.ev_id          users.usr_id      clients.cl_id          Need to be in one column of the results table

events.ev_name    users.usr_first    clients.cl_name      Needs to be displayed in the 2nd column of the results table

events.tableid        users.tableid        clients.tableid        Needs to be displayed in the 3rd column.


I need to search each table with a where statement like:

WHERE cl_name LIKE '%$trimmed%'
WHERE ev_name LIKE '%$trimmed%'
WHERE usr_first LIKE '%$trimmed%'


All rows from each table need to be displayed in the results table, the rows displayed in the results table needs to be limited.

and my php version is 5.1.56

 

I hope that makes it a bit clear, and my sincerest appologies again.

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.