简体   繁体   English

如何查询一对多实体?

[英]How to query One-to-Many entities?

In my system (PHP/MySql) I have a User table and Images table. 在我的系统(PHP / MySql)中,我有一个User表和Images表。 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 解决方案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 解决方案2

$users = query('SELECT *, (SELECT GROUP_CONCAT(id,path,size) FROM Images) as images FROM Users') $ users = query('SELECT *,(SELECT GROUP_CONCAT(id,path,size)FROM Images)作为来自用户的图像')

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 1:使用LEFT JOIN复制结果。

PS 2: Using INNER JOIN / JOIN , Users that don't have Images are ignored. PS 2:使用INNER JOIN / JOIN,没有图像的用户将被忽略。

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. 如果要使用INNER JOIN,但是请记住,对于大量数据,JOIN速度较慢,不建议使用。 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. 了解如何在需要时获取数据,我建议定义REST端点并进行Ajax请求并在需要Web客户端时获取数据。

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. 这是2表上所有的可能性查询,学生和付款。

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 将给出此结果-表付款中具有id_student的所有学生的姓名

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 将得到以下结果:LEFT表(学生)中所有在表付款中有或没有id_student的学生的姓名

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 会给出此结果-全部来自RIGHT表格付款,仅来自在表格付款中具有id_student的学生姓名

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 将给出此结果-表付款中具有id_student的所有学生的姓名

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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