我正在使用count并group by获得每天注册的订阅者数量:
SELECT created_at, COUNT(email)
FROM subscriptions
GROUP BY created at;
Run Code Online (Sandbox Code Playgroud)
结果:
created_at count
-----------------
04-04-2011 100
05-04-2011 50
06-04-2011 50
07-04-2011 300
Run Code Online (Sandbox Code Playgroud)
我想每天获得累计订阅者总数.我怎么得到这个?
created_at count
-----------------
04-04-2011 100
05-04-2011 150
06-04-2011 200
07-04-2011 500
Run Code Online (Sandbox Code Playgroud) 我想检索柏林时区记录的每日注册数量,但默认情况下我的数据库使用时区'-08'.如何select在指定时区时进行声明?
我有两组结果:
SELECT name, count(appearance) as countA from table where results = '1'
SELECT name, count(appearance) as countB from table where results = '2'
Run Code Online (Sandbox Code Playgroud)
我希望将它们并排组合,如下所示:
+---------+---------+---------+
| col_1 | countA | countB |
+---------+---------+---------+
| John | 3 | 1 |
| Mary | 1 | 2 |
| Gary | 2 | NULL |
| Sean | 4 | NULL |
| Mike | NULL | 6 |
+---------+---------+---------+
Run Code Online (Sandbox Code Playgroud)
我怎么做?
我正在使用count并group by了解每天有多少订阅者购买:
select count(s.email)
from subscriptions s, orders o
where o.subcription_id=s.id
group by s.created_at
Run Code Online (Sandbox Code Playgroud)
结果:
created_at count
------------------
04-04-2011 1
06-04-2011 2
07-04-2011 1
10-04-2011 5
11-04-2011 9
Run Code Online (Sandbox Code Playgroud)
但是我仍然希望空行返回为'0'.我该怎么做呢?请注意,我必须使用这两个表.
created_at count
------------------
04-04-2011 1
05-04-2011 0
06-04-2011 2
07-04-2011 1
08-04-2011 0
09-04-2011 0
10-04-2011 5
11-04-2011 9
Run Code Online (Sandbox Code Playgroud) 要调用此部分视图:
<% form_for(:subscription, :url => city_subscriptions_path(@city)) do |form| %>
<%= form.hidden_field :city_id, :value => @city.id %>
<%= form.text_field :email, :size => 30 %>
<%= form.submit "Email Me" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
由于我在不同的地方使用这个局部视图,如何更改调用者以便它将为form_for助手传递哈希?所以当调用帮助器时会是这样的:
<% form_for(:subscription, :url => city_subscriptions_path(@city), :html => {:id => 'main_input' }) do |form| %>
<%= form.hidden_field :city_id, :value => @city.id %>
<%= form.text_field :email, :size => 30 %>
<%= form.submit "Email Me" %>
<% end %>
Run Code Online (Sandbox Code Playgroud) 我有一个名为userswith a column 的表activated_at,我想通过检查列是否为null来计算已激活的用户数.然后像这样并排显示它们:
+----------+-----------+---------------+-------+
| Malaysia | Activated | Not Activated | Total |
+----------+-----------+---------------+-------+
| Malaysia | 5487 | 303 | 5790 |
+----------+-----------+---------------+-------+
Run Code Online (Sandbox Code Playgroud)
这是我的SQL:
select "Malaysia",
(select count(*) from users where activated_at is not null and locale='en' and date_format(created_at,'%m')=date_format(now(),'%m')) as "Activated",
(select count(*) from users where activated_at is null and locale='en' and date_format(created_at,'%m')=date_format(now(),'%m')) as "Not Activated",
count(*) as "Total"
from users
where locale="en"
and date_format(created_at,'%m')=date_format(now(),'%m');
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我必须指定所有where语句三次,这显然是多余的.我怎么能重构这个?
此致,MK.