title: How to perform join query in firebase database using android
body: <p>Here member’s key in Group is User’s key.
  Task: Want to fetch comida_id fro<code>enter code here</code>m User  based on Group’s member key. 
  Here we are trying to first fetch member keys from Group. While fetching member key we also want to fetch User and User’s comida_id while that member’s loop is going. So the scenario will be First we are going to fetch all keys from Group based on Group’s key ( for example createTime, groupName, members ). Now from that we will execute loop on members so will get member’s key. Now while fetching this member’s key in that loop, we want to fetch comida_id from User’s, which will be fetch in that loop only.</p>
  
  <p><strong>GroupKey -> get Data(createTime, groupName, members, restaurants) -> fetch members key and comida_id ( in Loop (get member key then fetch comida_id ))</strong></p>
  
  <p>*My Code</p>
  
  <pre><code>FirebaseDatabase mDatabase_gp = FirebaseDatabase.getInstance();
  mDatabase_gp.getReference("Group/"+group_key).addValueEventListener(
                  new ValueEventListener()
                  {
                      @Override
                      public void onDataChange(DataSnapshot dataSnapshot)
                      {
  
                          Map&lt;String, Object&gt; valuesMap = (HashMap&lt;String, Object&gt;) dataSnapshot.getValue();
  
                          Map userkey = (Map) valuesMap.get("members");
                          Log.d("Members", String.valueOf(userkey));
  
                          for (Object ke: userkey.keySet())
                          {
                              Log.d("runfirst","First Run");
  
                              String key = String.valueOf(ke.toString());
                              Log.d("Member key",key);
  
  
                              // HERE WHAT CORRESPONDS TO JOIN
                              FirebaseDatabase User = FirebaseDatabase.getInstance();
                              User.getReference("User/"+key).addValueEventListener(
                                      new ValueEventListener()
                                      {
                                          @Override
                                          public void onDataChange(DataSnapshot dataSnapshot)
                                          {
                                              Map&lt;String, Object&gt; valuesMap = (HashMap&lt;String, Object&gt;) dataSnapshot.getValue();
  
                                              String userid = String.valueOf(valuesMap.get("comida_id"));
                                              Log.d("comida_id",userid);
                                              Toast.makeText(ChatSectionActivity.this,userid,Toast.LENGTH_SHORT).show();
                                          }
                                          @Override
                                          public void onCancelled(DatabaseError databaseError)
                                          {
                                          }
                                      }
                              );
                          }
  
                          Log.d("runlast","Last Run");
                      }
                      @Override
                      public void onCancelled(DatabaseError databaseError)
                      {
  
                      }
                  }
          );
  </code></pre>
  
  <p>*</p>
  
  <p>In Above code we tried to achieve same thing but here this code will fetch all member keys from group first. After fetching all members it will fetch comida_ids in another loop.. So there will be like two different loops working to fetch comida_id from members. It’s not working like join query.</p>
  
  <p><strong>GroupKey -> get Data(createTime, groupName, members, restaurants) -> fetch members key (in Loop (get member key) after then fetch comida_id .</strong></p>
  
  <p>*1) I was try this solution - <a href="https://stackoverflow.com/questions/41135658/how-to-perform-join-query-in-firebase">How to perform join query in Firebase?</a></p>
  
  <p>**But get this error –</p>
  
  <pre><code>Incompatible types.  
  Required:   com.google.firebase.database.DatabaseReference 
  Found:  com.google.firebase.database.ValueEventListener 
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/ljlWo.png" rel="nofollow noreferrer">https://i.stack.imgur.com/ljlWo.png</a></p>