简体   繁体   English

从数据库查询结果

[英]Query result from database

I have two tables : galleries and shared galleries. 我有两个表:画廊和共享画廊。

Structure of galleries: (for storing images of individual students. One student contains multiple images) 画廊的结构:(用于存储单个学生的图像。一个学生包含多个图像)

id, student_id, classroom_id, image

Structure of shared_galleries: (for storing images which are common to all students in a classroom. One classroom contains many images): shared_galleries的结构:(用于存储教室中所有学生共有的图像。一个教室包含许多图像):

id,classroom_id,image

Other than these two tables I have students table and classrooms table. 除了这两个表,我还有学生表和教室表。 Students table store the classroom_id. 学生表存储教室ID。

I need to get a query so that I can display the images stored in 'galleries' for a student and those stored in shared gallery of the classroom in which that student belongs in a single page. 我需要查询,以便可以在一个页面中显示学生的“图库”中存储的图像以及该学生所属的教室的共享画廊中存储的图像。 How can I achieve this ? 我该如何实现? Something like this returns duplicated results : 这样的事情会返回重复的结果:

select galleries.id as gid, 
       shared_galleries.id as sid,
       galleries.student_id, galleries.classroom_id 
from galleries 
     inner join shared_galleries on galleries.classroom_id=shared_galleries.classroom_id 
where galleries.student_id=31 and galleries.classroom_id=28

You will need to join the student to the shared_galleries along the relations to get the right results 您将需要沿着关系将学生加入shared_galleries以获得正确的结果

SELECT 
    g.image AS image,
    0 AS is_shared
WHERE 
    g.student_id = :student_id
FROM
    galleries AS g
UNION
SELECT
    sg.image AS image,
    1 AS is_shared
FROM 
    shared_galleries AS sg
    LEFT JOIN classrooms AS c ON c.id = sg.classroom_id
    LEFT JOIN students AS s ON s.classroom_id = c.id
WHERE 
    s.id = :student_id

This should give you all the images for a student with :student_id , I've also added the is_shared column in case you need to know the origin of the image 这应该为您提供一个具有:student_id的学生的所有图像,我还添加了is_shared列,以防您需要了解图像的来源

Since your images are stored on a per-record basis (in both galleries and shared galleries, using a JOIN is not needed. Use a UNION instead: 由于您的图像是按记录存储的(在画廊和共享画廊中),因此不需要使用JOIN。请改用UNION:

SELECT 
    galleries.id AS ID, 
    galleries.student_ID AS StudentID, 
    galleries.classroom_id as ClassroomID, 
    galleries.image as Image 
WHERE 
    galleries.student_id = 31
UNION
SELECT
    shared_galleries.id AS ID, 
    NULL AS StudentID, 
    shared_galleries.classroom_id as ClassroomID, 
    shared_galleries.image as Image 
WHERE 
    shared_galleries.classroom_ID = 31

This will produce a list of records, with each record containing one image (since the number of images per student and the number if images per classroom is never consistent) 这将产生一个记录列表,每个记录包含一个图像(因为每个学生的图像数量和每个教室的图像数量不一致时的数量)

If you want do a little extra and ensure that your classroom images are always linked to the correct student, then declare a variable at the beginning to set the student_id value, and then use it in the second SELECT statement. 如果您想做一些额外的工作,并确保您的教室图像始终链接到正确的学生,请在开始时声明一个变量以设置student_id值,然后在第二个SELECT语句中使用它。

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

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