如何根据比较将整个结果查询分成几类?

时间:2017-08-01 10:45:25

标签: php mysql

我的MySQL知识仅限于最基本的知识,但今天我需要完成一项复杂的任务,而且我不知道如何执行以下操作:

我有一个包含三列的表:FOP ExampleFO2PDF Preparing... Input: XSL-FO (.\xml\fo\helloworld.fo) Output: PDF (.\out\ResultFO2PDF.pdf) Transforming... août 07, 2017 11:26:49 AM org.apache.fop.events.LoggingEventListener processEvent INFOS: Rendered page #1. org.apache.fop.pdf.PDFConformanceException: For PDF/A-1b, all fonts, even the base 14 fonts, have to be embedded! Offending font: /Helvetica at org.apache.fop.pdf.PDFFont.validate(PDFFont.java:170) at org.apache.fop.pdf.PDFFont.output(PDFFont.java:179) at org.apache.fop.pdf.PDFDocument.outputIndirectObject(PDFDocument.java:1053) at org.apache.fop.pdf.PDFDocument.streamIndirectObject(PDFDocument.java:1018) at org.apache.fop.pdf.PDFDocument.output(PDFDocument.java:994) at org.apache.fop.pdf.PDFDocument.outputTrailer(PDFDocument.java:1097) at org.apache.fop.render.pdf.PDFDocumentHandler.endDocument(PDFDocumentHandler.java:191) at org.apache.fop.render.intermediate.util.IFDocumentHandlerProxy.endDocument(IFDocumentHandlerProxy.java:187) at org.apache.fop.render.intermediate.IFRenderer.stopRenderer(IFRenderer.java:295) at org.apache.fop.area.RenderPagesModel.endDocument(RenderPagesModel.java:265) at org.apache.fop.area.AreaTreeHandler.endDocument(AreaTreeHandler.java:342) at org.apache.fop.fo.FOTreeBuilder.endDocument(FOTreeBuilder.java:170) at org.apache.xalan.transformer.TransformerIdentityImpl.endDocument(TransformerIdentityImpl.java:963) at org.apache.xerces.parsers.AbstractSAXParser.endDocument(Unknown Source) at org.apache.xerces.impl.XMLDocumentScannerImpl.endEntity(Unknown Source) at org.apache.xerces.impl.XMLEntityManager.endEntity(Unknown Source) at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source) at org.apache.xerces.impl.XMLEntityScanner.skipSpaces(Unknown Source) at org.apache.xerces.impl.XMLDocumentScannerImpl$TrailingMiscDispatcher.dispatch(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:485) at ConverterFO2PDFa.convertFO2PDF(ConverterFO2PDFa.java:104) at ConverterFO2PDFa.main(ConverterFO2PDFa.java:154) SEXSITUATION。这张表是为了说明有多少人是男性/女性,如果他们仍然活跃,他们在他们的部门有多少人(这些是其他专栏,但使用它们没有用)。

所以,如果我从这张表中取一条随机线,我就可以得到

NUMBER

我的目标是,当我执行我的SQL请求时,它会返回4"组"包含可能性的总和:有多少人是活跃的女性,退休女性,活跃男性和退休男性。我想结果会是这样的:

(
    [SEX] => 'FEMALE',
    [SITUATION] => 'ACTIVE',
    [NUMBER] => 198
) 

这是我用PHP处理结果的方法,如果它可以帮助你:

( 
    [active_women] => 8452,
    [retired_women] => 123,
    [active_men] => 9523,
    [retired_men] => 876
)

欢迎任何帮助,谢谢你提前

1 个答案:

答案 0 :(得分:2)

您可以在MySQL自身中使用条件聚合来处理此计算。

SELECT
    SUM(CASE WHEN SEX = 'FEMALE' AND SITUATION = 'ACTIVE'
             THEN NUMBER ELSE 0 END) AS active_women,
    SUM(CASE WHEN SEX = 'FEMALE' AND SITUATION = 'RETIRED'
             THEN NUMBER ELSE 0 END) AS retired_women,
    SUM(CASE WHEN SEX = 'MALE'   AND SITUATION = 'ACTIVE'
             THEN NUMBER ELSE 0 END) AS active_men,
    SUM(CASE WHEN SEX = 'MALE'   AND SITUATION = 'RETIRED'
             THEN NUMBER ELSE 0 END) AS retired_men
FROM yourTable

您也可以像在PHP中那样继续进行,但如果在汇总数据时出错,那么这将是费力的,也可能容易出错。