[英]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
. 这些表是
psthostess
和psttodo-uit
。
In my psthostess
I want to select the fields Code
and Name
. 在我的
psthostess
我要选择字段Code
和Name
。
In my psttodo-uit
I want to select: 在我的
psttodo-uit
我要选择:
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). 但是
psthostess
有Indienst = 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.