获取所有用户及其所有图片

时间:2017-01-10 11:29:04

标签: mysql join

我有2个mysql表:

  • 用户表
  • 包含每个用户的图库图像的表格

我的用户表格如下:

id | name
---------
1   Ryan
2   James
3   Dave

我的user_gallery_images表格如下:

id | user_id | image
--------------------
1    1         image.jpg
2    1         image2.jpg
3    2         image3.jpg
4    2         image4.jpg

我想知道是否有查询会检索所有用户,并获取该用户的所有图片。

预期结果应如下所示:

id | name | images
-------------------
1    Ryan   image.jpg,image2.jpg
2    James  image3.jpg,image4.jpg
3    Dave   

谢谢

3 个答案:

答案 0 :(得分:1)

这对你有用

SELECT u.id,u.name GROUP_CONCAT( g.image ) AS image 
FROM users u INNER JOIN user_gallery_images g ON u.user_id=g.id 
GROUP BY u.id

答案 1 :(得分:0)

public class MainActivity extends AppCompatActivity implements       Spinner.OnItemSelectedListener {
private Spinner spinner;
private ArrayList<String> trips;
private JSONArray result;
public static String SYSTEM_ID;
public static String CUSTOMER_ID;
public static String USER_ID;
public static String TRIP_TYPE;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    trips= new ArrayList<String>();
    this.spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(this);
    loadtrip();
}
public void loadtrip() {
    StringRequest stringRequest = new StringRequest(config.DATA_URL,
            new Response.Listener<String>() {
                public void onResponse(String response) {
                    JSONObject j = null;
                    try {
                        j = new JSONObject(response);
                        result = j.getJSONArray(config.JSON_ARRAY);
                        getStudents(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }) {
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put(SYSTEM_ID, "12");
            params.put(CUSTOMER_ID, "3513");
            params.put(USER_ID, "124");
            params.put(TRIP_TYPE, "Open");
            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}
private void getStudents(JSONArray j){
    for(int i=0;i<j.length();i++){
        try {
            JSONObject json = j.getJSONObject(i);
            trips.add(json.getString(config.DATA_URL));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,trips));
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String item = parent.getItemAtPosition(position).toString();
    Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {


}


}

答案 2 :(得分:0)

您必须使用LEFT JOIN而不是INNER JOIN,因为您想要检索没有图像的David。你需要使用GROUP_CONCAT

SELECT u.id, u.name, GROUP_CONCAT(image) from users u 
LEFT JOIN user_gallery_images g 
ON u.id = g.user_id GROUP by u.id

请注意,只有在users.id上有PRIMARY KEY时,此查询才适用于mysql 5.7。将在mysql上工作&lt; 5.7无论主键如何