简体   繁体   English

SELECT INNER JOIN仅返回一个结果

[英]SELECT INNER JOIN only returns one result

I'm having trouble selecting some data from 2 tables in my database. 我无法从数据库的2个表中选择一些数据。

The tables are psthostess and psttodo-uit . 这些表是psthostesspsttodo-uit

In my psthostess I want to select the fields Code and Name . 在我的psthostess我要选择字段CodeName
In my psttodo-uit I want to select: 在我的psttodo-uit我要选择:

  • sum of PB=1 PB = 1
  • sum of PG=1 PG = 1
  • sum of PA=1 PA总和= 1
  • sum of h.GoedkeuringDoorNew=GF h.GoedkeuringDoorNew = GF的总和
  • sum of h.GoedkeuringDoorNew=SB h.GoedkeuringDoorNew = SB的总和
  • sum of h.GoedkeuringDoorNew=VIA h.GoedkeuringDoorNew = VIA的总和
  • sum of h.Blanco h.Blanco的总和

This is my query: 这是我的查询:

SELECT p.Code, p.Name, sum(h.PB = 1), sum(h.PG = 1), sum(h.PA = 1), 
       sum(h.GoedkeuringDoorNew = 'GF'), sum(h.GoedkeuringDoorNew = 'SB'),  
       sum(h.GoedkeuringDoorNew = 'VIA'), sum(h.Blanco)
   FROM psthostess p
   INNER JOIN `psttodo-uit` h ON h.`Hostess Code` = p.Code
   WHERE p.Indienst = 1

The problem is that I always get a result of one row. 问题是我总是得到一行结果。 But there are multiple rows in psthostess with Indienst = 1 (WHERE). 但是psthostessIndienst = 1 (WHERE)的多行。 How can I fix this? 我怎样才能解决这个问题?

You need to group it by non-aggregated columns. 您需要按未聚合的列对其进行分组。

SELECT p.Code, p.Name, sum(h.PB = 1), 
sum(h.PG = 1), sum(h.PA = 1), sum(h.GoedkeuringDoorNew = 'GF'), 
sum(h.GoedkeuringDoorNew = 'SB'), sum(h.GoedkeuringDoorNew = 'VIA'), sum(h.Blanco)
FROM psthostess p
INNER JOIN `psttodo-uit` h ON h.`Hostess Code` = p.Code
WHERE p.Indienst = 1
group by p.Code, p.Name

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

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