Solved

Including items from various tables in a query MySQL.

Posted on 2011-09-07
10
324 Views
Last Modified: 2012-05-12
Hi,

I need to generate a MySQL query based on a field from a table. My database contains 2 tables. On each record found by the query  I need to include data from the other table. This other data should be linked based on a specific field on the record found by the main query.
Hope I have explained myself.

Regards
0
Comment
Question by:lxfdirs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 4

Expert Comment

by:Ara-
ID: 36494982
Your question is kinda vague, but you can do a new SELECT in the WHERE-clause.

E.g.:
SELECT name FROM table1 WHERE id=(SELECT id FROM table2 WHERE field=3)
0
 
LVL 27

Accepted Solution

by:
Cornelia Yoder earned 168 total points
ID: 36495017
SELECT t1.FieldA, t2.FieldB FROM Table1 as t1, Table2 as t2 WHERE t1.KeyA=t2.KeyB
0
 
LVL 21

Assisted Solution

by:K V
K V earned 166 total points
ID: 36495066
watch out for join.
0
Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

 
LVL 4

Assisted Solution

by:Ara-
Ara- earned 166 total points
ID: 36495090
Ah. I can see I read the question wrong.

yodercm's example with JOIN:

SELECT t1.FieldA, t2.FieldB FROM Table1 as t1 JOIN Table2 as t2 ON t1.KeyA=t2.KeyB
0
 
LVL 9

Expert Comment

by:jeff_01
ID: 36495207
Perhaps you could provide a bit more info and some sample data but I think you are trying to do this below ?


select * from tableA, tableB where tableA.ID = tableB.ID

0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 36495376
Don't use JOIN unless you absolutely have to, it's much messier and complicated to understand and debug.
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 36509863
            Hi!

To speed up the query make sure that you have indexes on both tables
that matches the columns you use in the where clauses of the query . :)

Regards,
    Tomas Helgi
0
 

Author Comment

by:lxfdirs
ID: 36514891
Guys:

Thank you all for the suggestions.
I will be checking on them and come back to you with the accepted solution.

Regards
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 37349210
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question