mysql根据cont的结果计算行数

时间:2017-01-21 18:00:50

标签: mysql

我不确定如何解释这一点,并且不知道我将使用哪种类型的SQL问题。 我有两个问题,我必须结合起来。第一个查询计算不同的湖泊并进行一些求和。

我的其他查询计数数据按日期,用户和湖泊分开,并输出所选用户在所选湖泊上出现的次数

My Querys看起来像这样

1

SELECT lakes.*, mycall, sm, mode, SUM(IF(sm != '', 1,0)) AS 'a_lake', COUNT(sm) AS 'total' FROM lakes LEFT JOIN ss_log ON ss_log.sm = lakes.id WHERE mycall = '$ListCall' AND ss_log.sm !='' AND ss_log.sm = lakes.id AND conf = '1' GROUP BY sm ORDER BY a_lake DESC

2

SELECT DISTINCT mycall, tid, count(distinct date(tid)) as datum, sm FROM ss_log WHERE mycall = '$ListCall' and sm = 'lakes.id'

这两个Querys可以单独使用,但我如何将它们组合在一起呢? 也就是说,查询1的结果应该是查询2的输入。查询应输出与两个查询相同的结果,但作为组合结果。

1 个答案:

答案 0 :(得分:0)

我自己想出来了。 Självärbästadräng! (正如我们在瑞典所说)

SELECT lakes.*, mycall, sm, mode, tid ,count(distinct date(tid)) as visits, SUM(IF(sm != '', 1,0)) AS 'a_lake', COUNT(sm) AS 'total' FROM lakes LEFT JOIN ss_log ON ss_log.sm = lakes.id WHERE mycall = '$ListCall' AND ss_log.sm !='' AND ss_log.sm = lakes.id AND conf = '1' GROUP BY sm ORDER BY a_lake DESC