Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

Including items from various tables in a query MySQL.

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
lxfdirs
Asked:
lxfdirs
3 Solutions
 
Ara-Commented:
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
 
Cornelia YoderArtistCommented:
SELECT t1.FieldA, t2.FieldB FROM Table1 as t1, Table2 as t2 WHERE t1.KeyA=t2.KeyB
0
 
K VDatabase ConsultantCommented:
watch out for join.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Ara-Commented:
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
 
jeff_01Commented:
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
 
Cornelia YoderArtistCommented:
Don't use JOIN unless you absolutely have to, it's much messier and complicated to understand and debug.
0
 
Tomas Helgi JohannssonCommented:
            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
 
lxfdirsAuthor Commented:
Guys:

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

Regards
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now