Knowledgebase

Selecting data from two different tables in phpmyadmin

Posted by globalcashsite, 06-29-2009, 05:37 AM
Guys I have two tables, A = Client table (client_id, client_name fields) B = History table (amount field) I want to select all clients from Client tables who have any record/amount in History Table. Please let me know how to do this? Thank you GCS

Posted by 01globalnet, 06-29-2009, 07:22 AM
You should have an associated key for both tables. A: client_id, B:client_id

Posted by mioot, 06-29-2009, 07:44 AM
run this query in phpmyadmin SELECT client.name,history.amount FROM HISTORY LEFT JOIN client ON client .client_id = history.client_id

Posted by jstanden, 06-29-2009, 01:47 PM
If there's always going to be a foreign key on 'client_id' between the tables you should use an INNER JOIN rather than a LEFT JOIN. The latter will also include rows which don't exist in both tables (a client without history rows, or orphaned history rows with no client). http://en.wikipedia.org/wiki/Referential_integrity

Posted by HivelocityDD, 06-30-2009, 10:02 AM
It is always better to use a foreign key in table B if you dont have one there. Also use inner join ie (where a.id=b.fid) instead of left join to avoid unnecessary records. You can run the sql query in phpmyadmin using the editSQL option



Was this answer helpful?

Add to Favourites Add to Favourites

Print this Article Print this Article

Also Read
Named shutting down (Views: 611)
gplhost problems? (Views: 614)
www.5wire.co.uk (Views: 632)
Startwebhost down? (Views: 590)


Language:

Client Login

Email

Password

Remember Me

Search