Php 添加where条件时mysql查询不工作 选择tm.MAGAZINE\u ID,`MAGAZINE\u NAME`, 当tsd.no\U of_问题为空时,则1个其他tsd.no\U of_问题 以订阅类型结束, 总计(单位)为总销售额, 金额(当客户的货币=美元时,则tu.developer的收益*tu.units,否则0结束)作为收入=美元, 总和(当客户的货币为CAD时,则tu.developer继续*tu.units否则0结束)作为收入, 金额(当客户的货币=人民币时,则tu.developer的收益*tu.units的其他0结束)作为收入=人民币, 作为收入欧元的金额(当客户的货币=欧元时,则tu.developer继续*tu.units否则0结束), 金额(当客户的货币=英镑时,则tu.developer继续*tu.units否则0结束)作为收入=英镑, 作为收入的总和(当客户的货币为AUD时,则tu.DEVIDER继续*tu.units ELSE 0 END), 作为收入的总和(当客户\u currency='MXN'然后tu.developer\u继续*tu.units否则0结束时), 总和(当客户的货币为CHF时,则tu.developer继续*tu.units否则0结束)作为收入CHF, 金额(当客户的货币=NZD时,则tu.developer继续*tu.units否则0结束)作为收入, 将金额(当客户的货币=DKK时,则tu.developer继续*tu.units否则0结束)作为收入, 金额(当客户的货币为“NOK”时,则tu.developer继续*tu.units否则0结束)作为收入(NOK), 将金额(当客户的货币为日元时,则tu.developer继续*tu.units否则0结束)作为收入日元, 金额(当客户的货币=瑞典克朗时,则tu.developer继续*tu.units否则0结束)作为收入(瑞典克朗), 金额(当客户的货币=新加坡元时,则tu.developer继续*tu.units否则0结束)作为收入, 总和(当客户的货币为TWD时,则tu.developer继续*tu.units否则0结束)作为收入, 金额(当客户的货币为港币时,则tu.developer收益*tu.units其他0结束)作为收入(港币), 金额(当客户的货币=人民币时,则tu.developer的收益*tu.units的其他0结束)作为收入=人民币, 作为收入印尼盾的金额(当客户的货币=印尼盾时,则tu.developer继续*tu.units否则0结束), 金额(当客户的货币为AED时,则tu.developer继续*tu.units否则0结束)作为收入, 总和(当客户的货币=SAR时,则tu.developer继续*tu.units否则0结束)作为收入, 总和(当客户的货币为ILS时,则tu.developer继续*tu.units否则0结束)作为收入ILS, 总和(当客户的货币为卢布时,则tu.developer继续*tu.units否则0结束)作为收入卢布, 将金额(当客户的货币=ZAR时,则tu.developer继续*tu.units否则0结束)作为收入, 合计(当customer\u currency='TRY'然后tu.developer\u继续*tu.units ELSE 0 END)作为收入, 金额(当客户的货币为卢比时,则tu.developer的收益为*tu.units,否则为0结尾)作为收入为卢比 来自“tbl_itunes_report”tu 左键在tsd.subscription\u key=tu.sku\u key和((tu.begin\u date>=tsd.start\u date和tu.begin\u date=tsd.start\u date和tsd.end\u date='0000-00')上加入tbl\u杂志\u订阅\u dtl tsd 左加入tbl\U杂志\U订阅ts ON ts.magazine\U订阅\U id=tsd.subs\U id左加入tbl\U杂志\U发行ti ON ti.PurchaseKey=tu.sku\u key和ti.OS\u选择=0 在tm.magage\u id=ts.magage\u id或tm.magage\u id=ti.magage\u id上左连接tbl\u magazine tm 其中“产品类型”标识符类似于“IA%” 和(tsd.subscription\u密钥不为NULL或ti.PurchaseKey不为NULL) 和tu.report_from='ecmedia'和'units`>0和tu.begin_date>=“2013-12-01” 和tu.begin_date

Php 添加where条件时mysql查询不工作 选择tm.MAGAZINE\u ID,`MAGAZINE\u NAME`, 当tsd.no\U of_问题为空时,则1个其他tsd.no\U of_问题 以订阅类型结束, 总计(单位)为总销售额, 金额(当客户的货币=美元时,则tu.developer的收益*tu.units,否则0结束)作为收入=美元, 总和(当客户的货币为CAD时,则tu.developer继续*tu.units否则0结束)作为收入, 金额(当客户的货币=人民币时,则tu.developer的收益*tu.units的其他0结束)作为收入=人民币, 作为收入欧元的金额(当客户的货币=欧元时,则tu.developer继续*tu.units否则0结束), 金额(当客户的货币=英镑时,则tu.developer继续*tu.units否则0结束)作为收入=英镑, 作为收入的总和(当客户的货币为AUD时,则tu.DEVIDER继续*tu.units ELSE 0 END), 作为收入的总和(当客户\u currency='MXN'然后tu.developer\u继续*tu.units否则0结束时), 总和(当客户的货币为CHF时,则tu.developer继续*tu.units否则0结束)作为收入CHF, 金额(当客户的货币=NZD时,则tu.developer继续*tu.units否则0结束)作为收入, 将金额(当客户的货币=DKK时,则tu.developer继续*tu.units否则0结束)作为收入, 金额(当客户的货币为“NOK”时,则tu.developer继续*tu.units否则0结束)作为收入(NOK), 将金额(当客户的货币为日元时,则tu.developer继续*tu.units否则0结束)作为收入日元, 金额(当客户的货币=瑞典克朗时,则tu.developer继续*tu.units否则0结束)作为收入(瑞典克朗), 金额(当客户的货币=新加坡元时,则tu.developer继续*tu.units否则0结束)作为收入, 总和(当客户的货币为TWD时,则tu.developer继续*tu.units否则0结束)作为收入, 金额(当客户的货币为港币时,则tu.developer收益*tu.units其他0结束)作为收入(港币), 金额(当客户的货币=人民币时,则tu.developer的收益*tu.units的其他0结束)作为收入=人民币, 作为收入印尼盾的金额(当客户的货币=印尼盾时,则tu.developer继续*tu.units否则0结束), 金额(当客户的货币为AED时,则tu.developer继续*tu.units否则0结束)作为收入, 总和(当客户的货币=SAR时,则tu.developer继续*tu.units否则0结束)作为收入, 总和(当客户的货币为ILS时,则tu.developer继续*tu.units否则0结束)作为收入ILS, 总和(当客户的货币为卢布时,则tu.developer继续*tu.units否则0结束)作为收入卢布, 将金额(当客户的货币=ZAR时,则tu.developer继续*tu.units否则0结束)作为收入, 合计(当customer\u currency='TRY'然后tu.developer\u继续*tu.units ELSE 0 END)作为收入, 金额(当客户的货币为卢比时,则tu.developer的收益为*tu.units,否则为0结尾)作为收入为卢比 来自“tbl_itunes_report”tu 左键在tsd.subscription\u key=tu.sku\u key和((tu.begin\u date>=tsd.start\u date和tu.begin\u date=tsd.start\u date和tsd.end\u date='0000-00')上加入tbl\u杂志\u订阅\u dtl tsd 左加入tbl\U杂志\U订阅ts ON ts.magazine\U订阅\U id=tsd.subs\U id左加入tbl\U杂志\U发行ti ON ti.PurchaseKey=tu.sku\u key和ti.OS\u选择=0 在tm.magage\u id=ts.magage\u id或tm.magage\u id=ti.magage\u id上左连接tbl\u magazine tm 其中“产品类型”标识符类似于“IA%” 和(tsd.subscription\u密钥不为NULL或ti.PurchaseKey不为NULL) 和tu.report_from='ecmedia'和'units`>0和tu.begin_date>=“2013-12-01” 和tu.begin_date,php,mysql,performance,query-performance,Php,Mysql,Performance,Query Performance,除了正确的索引之外,因为您使用的是外部联接,所以您很可能希望将应用于左联接(tm.publisher_id=120)右侧表的任何条件从该联接的WHERE子句移动到子句上的 。。。 左连接tbl_杂志tm 打开(tm.CARGINE_id=ts.CARGINE_id或 tm.magazine_id=ti.magazine_id) 和tm.publisher_id=120 ... 您应该查找mysql的“解释”功能,它可以帮助您了解为什么查询需要更长时间。您的数据库中是否有在tm.publisher

除了正确的索引之外,因为您使用的是外部联接,所以您很可能希望将应用于
左联接
tm.publisher_id=120
)右侧表的任何条件从该联接的
WHERE
子句移动到
子句上的

。。。
左连接tbl_杂志tm
打开(tm.CARGINE_id=ts.CARGINE_id或
tm.magazine_id=ti.magazine_id)
和tm.publisher_id=120
...

您应该查找mysql的“解释”功能,它可以帮助您了解为什么查询需要更长时间。您的数据库中是否有在
tm.publisher\u id
上创建的索引?如果您不这样做,这会大大降低这样的查询的速度。否则,@JoeT所说的就是最好的选择,因为您使用了许多聚合函数、内嵌条件语句、多个连接,以及一个执行排序的
orderby
语句,所有这些都会使查询速度变慢。这是一个非常糟糕的查询。你确定没有比这更简单的方法了吗?这本身并没有错,但如果这个查询很慢,您不应该感到惊讶;它必须做大量的工作才能产生结果。桌子越大,速度就越慢。