Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax for MySql query on related tables 2

Status
Not open for further replies.

Domino2

Technical User
Jun 8, 2008
475
GB
I am trying to find a straightforward query method (no php or anything else) for related tables

Table1
ID1 Primary Key
Name

Table2
ID1 (foreign key)
ID2 Primary key
Other data

I am selecting the primary key number of Table1 from a listbox list.

Thanks
 
I am trying to find a straightforward query method (no php or anything else) for related tables

I am selecting the primary key number of Table1 from a list box list.

This 2 statements contradict each other.

If you are selecting the ID's from a list box then you need PHP or something to connect to the DB and perform the query.
As MYSQL cannot produce any type of list box.

I'm guessing what you mean is you want a single query to join the tables.

Try:

Code:
SELECT table1.fieldname1, table1.fieldname2, table2.fieldname1, table2,fieldname2 ... FROM table1, table2 WHERE table1.id=table2.id AND table1.id=$idfromlist;








----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
phil, old-style comma joins are deprecated and ... old style

please, use JOIN syntax :)

SELECT table1.fieldname1
, table1.fieldname2
, table2.fieldname1
, table2.fieldname2 ...
FROM table1
[blue]INNER
JOIN table2
ON table2.id = table1.id[/blue]
WHERE table1.id = $idfromlist;



r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Many thanks to you both. I looked for hours on the internet for what I wanted that did not have PHP reference. I am trying to fill a datagridview control in VB.Net connected to MySql server. All the references I found were all creating tables etc, and SQL to a single table. I now have the syntax for my query from you, and thanks. Now I will go over to the VB.Net forum to fire more questions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top