我正在尝试访问两个表的数据库。在商店中,我保留了一个具有
的事件的列表Table Event
id, name,datei,houri, dateF,Hourf ,capacity, age ,description,image,url, local
Table Assistance
visibility, event, client
要访问具有我的ID的事件列表
SELECT * from event where local = '1'
要访问我参加的活动的人数
SELECT count (*) as assistants FROM Assistance WHERE event = '1'
但是把我拥有的两件事放在一起
SELECT e. *, COUNT (a.client) AS asis FROM event e LEFT JOIN assistance to ON e.eid = a.event where e.locales = '1' GROUP BY a.evento
在后者中,他带给我他想要的事件,有时仅在他有帮助的时候带给我。
使用mysql,所有连接均不可用。
我尝试调试尝试其他选项,但没有成功。如果有必要做出贡献或澄清,请告诉我。数字1是一个例子。
答案 0 :(得分:1)
您正在按import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.validation.MessageInterpolatorFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
import javax.validation.Validator;
@Configuration
public class ValidationConfiguration {
public ValidationConfiguration() {
}
@Bean(name = "overriddenValidator")
public Validator validator() {
LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
factoryBean.setParameterNameDiscoverer(new CustomParameterNameDiscoverer());
MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory();
factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
return factoryBean;
}
@Bean
public static MethodValidationPostProcessor methodValidationPostProcessor(Environment environment, @Qualifier("overriddenValidator") Validator validator) {
validator.forExecutables();
MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
boolean proxyTargetClass = (Boolean) environment.getProperty("spring.aop.proxy-target-class", Boolean.class, true);
processor.setProxyTargetClass(proxyTargetClass);
processor.setValidator(validator);
return processor;
}
}
的 second 表中的一列进行汇总。这可能是LEFT JOIN
。您想按第一个表进行汇总;并且如果您使用单列,则该列应该是主键或唯一键。
所以:
NULL
答案 1 :(得分:0)
要从相关表中获取计数,我们可以在选择列表中进行相关子查询
SELECT e.*
, ( SELECT COUNT(1)
FROM assistance a
WHERE a.event = e.eid
) AS asis
FROM event e
WHERE e.locales = '1'
或者我们可以在嵌入式视图中进行聚合,然后加入该视图
SELECT e.*
, IFNULL(c.cnt,0) AS asis
FROM event e
LEFT
JOIN ( SELECT a.event
, COUNT(1) AS cnt
FROM assistance a
GROUP
BY a.event
) c
ON c.event = e.eid
WHERE e.locales = '1'
问题尚不清楚规范(即查询应返回的内容),只有残破的SQL可以通过,我们只是在猜测规范。