Jump to content

Querying 2 tables to return results.


Strydaris

Recommended Posts

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!!

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.