Strydaris Posted April 20, 2007 Share Posted April 20, 2007 Hey there!! I am kinda new to this MYsql stuff and PHP stuff but I am learning alot as I go. Anyways I am having troubles trying to querying 2 tables. Let me try to explain. I have 2 tables that I am trying to query 2 things from both. For example I have Table 1 with FieldA and FieldB in them and Table 2 with FieldA and FieldC in it. I am trying to use Table1.FieldA and Table2.FieldA to link Table1.FieldB and Table2.FieldC information together. Not sure if thats how you go about doing it but it seems logical to me to search for a similar item from both Table1 and Table 2 to link different items together in the query. Also trying to add a count in there so I can count the number of items. Maybe a better explaination is in order. Table1.FieldA and Table2.FieldA are equal values. Table1.FieldB holds different types of information but I want to filter most of that out and just use a preset value (Lets say its INFO1). Since Table1 now has some filtered values I want to go to Table 2 and use the left over values from Table1.FieldA to find the same values in Table2.FieldA and retrieve the data from Table2.FieldC. I also need to take a count of the like values pulled from Table2.FieldC. LOL now that I through that out on the table this is as far as I have got on my MYsql Query SELECT table1.fielda, table1.fieldb, table2.fieldc, COUNT(table.fieldc ) FROM table1, table2 WHERE table1.fielda=table2.fielda GROUP BY table2.fieldc This is close but not yet there. Problem still is that I dont know how to stop it from gathering the unwanted information from Table1.FieldB I know adding in WHERE tabel1.fieldb = 'INFO1" should work but then I run into the problem of not having the WHERE available to filter the data from from Table1.FieldB. I hope this isnt to confusing and that someone understands enough to help me out!! Quote Link to comment https://forums.phpfreaks.com/topic/47823-querying-2-tables-to-return-results/ Share on other sites More sharing options...
btherl Posted April 20, 2007 Share Posted April 20, 2007 How about SELECT table1.fielda, table1.fieldb, table2.fieldc, COUNT(table.fieldc ) FROM table1, table2 WHERE table1.fielda=table2.fielda AND table1.fieldb = 'INFO1' GROUP BY table2.fieldc You can add as many conditions as you want using AND. Mysql will choose which order to apply them in, but the results will always be identical. If you want to do filtering after grouping, you can use HAVING, but it sounds like you want to filter before grouping. Quote Link to comment https://forums.phpfreaks.com/topic/47823-querying-2-tables-to-return-results/#findComment-233743 Share on other sites More sharing options...
bubblegum.anarchy Posted April 20, 2007 Share Posted April 20, 2007 The more I read the less I understood! Quote Link to comment https://forums.phpfreaks.com/topic/47823-querying-2-tables-to-return-results/#findComment-233747 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.