简体   繁体   English

SQL - 在子查询中传递列

[英]SQL - Passing column in subquery

my command:我的命令:

Select sobe.Id, sobe.Naziv, sobe.Opis, sobe.Kat, (Select Group_CONCAT(studenti.Ime,studenti.Prezime) ImePrezime FROM studentsoba LEFT JOIN studenti ON studentsoba.Id_Sobe=sobe.Id AND studenti.JMBAG = studentsoba.JMBAG) AS ImePrezime from Sobe

This gives me this error:这给了我这个错误:

1054 - Unknown column 'sobe.Id' in 'on clause' 1054 - 'on 子句'中的未知列'sobe.Id'

I see what the problem is but I cant figure out how to fix it.我知道问题出在哪里,但我不知道如何解决它。 I want to pass sobe.Id inside of this subquery:我想在这个子查询中传递sobe.Id

(Select Group_CONCAT(studenti.Ime,studenti.Prezime) ImePrezime FROM studentsoba LEFT JOIN studenti ON studentsoba.Id_Sobe=sobe.Id AND studenti.JMBAG = studentsoba.JMBAG)

I want to check which students are in that room.我想查看那个房间里有哪些学生。 Word soba means room and JMBAG is like personal number for each student单词 soba 意味着房间,JMBAG 就像每个学生的个人号码

Try moving it to a WHERE clause:尝试将其移至WHERE子句:

Select s.Id, sobe.Naziv, s.Opis, s.Kat,
       (Select Group_CONCAT(si.Ime, si.Prezime) as ImePrezime
        from studentsoba ss LEFT JOIN
             studenti si
             on si.JMBAG = ss.JMBAG
        where ss.Id_Sobe = s.Id 
       ) AS ImePrezime
from Sobe s;

The correlated subquery should be fine.相关的子查询应该没问题。 I suppose the correlation clause cannot be in the on clause -- I normally put it in the where clause anyway.我想相关子句不能on子句中——我通常把它放在where子句中。

Also note that I introduced table aliases so the query is easier to write and to read.另请注意,我引入了表别名,因此查询更易于编写和阅读。

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

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