简体   繁体   中英

How to query One-to-Many entities?

In my system (PHP/MySql) I have a User table and Images table. One User can have many Images.

I have a page where a need to list all Users(even if the user don't have Images) and all Images, for each user. How can I get this?

Solution 1

  $users = query('SELECT * FROM Users') 

then

  foreach($users as $user) $user->images = query('SELECT id, path, size FROM Images WHERE user_id = ' . $user->id) 

Solution 2

$users = query('SELECT *, (SELECT GROUP_CONCAT(id,path,size) FROM Images) as images FROM Users')

then

  foreach($users as $user) list($id,$path, $size) = explode(',', $user->images) $user['images'] = ['id' => $id, 'path' => $path, 'size' => $size]; 

There is a better way to acomplish this?

PS 1: Using LEFT JOIN duplicate the results.

PS 2: Using INNER JOIN / JOIN , Users that don't have Images are ignored.

Here, the example (Remember Inner join is the Intersection of both table)

SELECT
Users.id as user_id,
Images.id as image_id,
Images.path,
Images.size
FROM
Users
INNER JOIN Images ON Users.id = Images.user_id

IF you want to use INNER JOIN, But remember for large and many data, JOINs are slower and not recommended. Learn how to get data when you need, I recommend define REST endpoints and make ajax requests and get data when your web client need.

If you dont want to miss any user and also dont want duplicate then see following links.
Update:
I see your updated post. this Link will help you.
and more precisely the answer here Left Join Without Duplicate

and Two tables, whit one to many relationship. How to join values without duplicate rows?

SELECT User.* Images.id as images_id, path, size
FROM Images
LEFT JOIN on User user.id = Images.user_id

Here is all posibility query on 2 table, students and payment.

Tables:   students                payment
    id_student  name        id  id_student datepayment
    1         Lisa         1    1        2017-01-01
    2                      2    1        2017-02-03
    3         Asher        3    2        2017-03-05
    4         Lee          4    1        2017-03-03

This query:

select students.name, payment.datepayment
FROM students, payment
where students.id_student = payment.id_student;

will give this result - all student's name which has id_student in table payment

name    datepayment
Lisa    2017-01-01
Lisa    2017-02-03
Lisa    2017-03-03
        2017-03-05

This query:

SELECT s.name, p.datepayment
FROM
students s
LEFT OUTER JOIN payment p ON s.id_student = p.id_student;

will give this result: all student's name from LEFT table (students) which has or has not id_student in table payment

name    datepayment
Lisa    2017-01-01
Lisa    2017-02-03
        2017-03-05
Lisa    2017-03-03
Asher       NULL
Lee         NULL

This query:

SELECT s.name, p.datepayment
FROM
students s
RIGHT OUTER JOIN payment p ON s.id_student = p.id_student;

will give this result - all from RIGHT table payment and only student's name which has id_student in table payment

name    datepayment
Lisa    2017-01-01
Lisa    2017-02-03
        2017-03-05
Lisa    2017-03-03

This query:

SELECT s.name, p.datepayment
FROM
students s
INNER JOIN payment p ON s.id_student = p.id_student;

will give this result - all student's name which has id_student in table payment

name    datepayment
Lisa    2017-01-01
Lisa    2017-02-03
Lisa    2017-03-03

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM