PDA

View Full Version : Pulling data from two different tables and displaying in search results


awayne96
10-19-2009, 12:09 AM
I have several tables in my database but what I am looking to do is pull data from 2 different tables.... One table I need just a few fields and the other table will supplythe rest of the data for the search. I have tried to search all over for help and can't seem to find what I am looking for.

Thanks!

Old Pedant
10-19-2009, 05:42 AM
There is no way we can tell from that description whether you *can* do this with one query.

You don't tell us how--if at all--the data in the two tables are related.

If the data is completely unrelated you will need (and want) to make two queries.

If the data is related, you *probably* want to use a JOIN of some kind. But what kind and whether that's the right answer depends entirely on your data.

And I hope you will pardon me for saying so, but if you have indeed searched "all over" then you need to learn how to use Google more effectively. Most any of hundreds--if not thousands--of web sites would teach you DB design and/or Normalization and/or JOIN queries.

awayne96
10-23-2009, 06:36 AM
Thanks for the response and sorry for my delay since I was working on a few other things for my site.

But regarding the tables in the database... I have table a that is the profile table for all the members and then there is a table b that has the data for the postings that members have.

I need just a few rows out of table a to be displayed with the search results. ALL the data in the results is coming from table b except for what I need out of table a.

I hope that explains it well enough for what I'm looking for and my databases.

Thanks!

Coyote6
10-23-2009, 07:17 PM
It helps if you actually show more info and their relationships like OP said but from what I can tell is that you have two tables that probably look like the below (just guessing mind you):


Users
id | name | etc...

Posts
id | user_id | title | text


So I guess you would want to pull the post data and the user data at the same time.


SELECT p.title, p.text, u.name FROM Users as u, Posts as p WHERE u.user_id=p.user_id && user_id=your_inputted_id;


There are so many ways to do this... It really should not be hard to find the answer. Things like this are all over the web and this forum.

Old Pedant
10-23-2009, 08:45 PM
You also said "first few rows", so that *probably* means just adding a LIMIT clause in MySQL.

But you really should show us sample table data and then a sample of the desired results. The "gozinta and gozouta" as an old programmer friend used to say.