如果语句执行不同的查询

时间:2016-12-18 09:55:26

标签: mysql sql

如果当前日期是例如=' 2016-12-18'我尝试编写将运行查询A的查询如果没有,请运行查询B.

查询示例:

2016-12-18T01:57:34.469373+00:00 app[web.1]: #<RuntimeError: Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml`>
2016-12-18T01:57:34.469375+00:00 app[web.1]: /app/vendor/bundle/ruby/2.2.0/gems/railties-5.0.0.1/lib/rails/application.rb:513:in `validate_secret_key_config!'
2016-12-18T01:57:34.469376+00:00 app[web.1]: /app/vendor/bundle/ruby/2.2.0/gems/railties-5.0.0.1/lib/rails/application.rb:246:in `env_config'
2016-12-18T01:57:34.469377+00:00 app[web.1]: /app/vendor/bundle/ruby/2.2.0/gems/railties-5.0.0.1/lib/rails/application.rb:521:in `build_request'
2016-12-18T01:57:34.469377+00:00 app[web.1]: /app/vendor/bundle/ruby/2.2.0/gems/railties-5.0.0.1/lib/rails/engine.rb:693:in `build_request'
2016-12-18T01:57:34.469378+00:00 app[web.1]: /app/vendor/bundle/ruby/2.2.0/gems/railties-5.0.0.1/lib/rails/engine.rb:521:in `call'
2016-12-18T01:57:34.469379+00:00 app[web.1]: /app/vendor/bundle/ruby/2.2.0/gems/puma-3.6.2/lib/puma/configuration.rb:225:in `call'
2016-12-18T01:57:34.469379+00:00 app[web.1]: /app/vendor/bundle/ruby/2.2.0/gems/puma-3.6.2/lib/puma/server.rb:578:in `handle_request'
2016-12-18T01:57:34.469380+00:00 app[web.1]: /app/vendor/bundle/ruby/2.2.0/gems/puma-3.6.2/lib/puma/server.rb:415:in `process_client'
2016-12-18T01:57:34.469381+00:00 app[web.1]: /app/vendor/bundle/ruby/2.2.0/gems/puma-3.6.2/lib/puma/server.rb:275:in `block in run'
2016-12-18T01:57:34.469381+00:00 app[web.1]: /app/vendor/bundle/ruby/2.2.0/gems/puma-3.6.2/lib/puma/thread_pool.rb:116:in `call'
2016-12-18T01:57:34.469382+00:00 app[web.1]: /app/vendor/bundle/ruby/2.2.0/gems/puma-3.6.2/lib/puma/thread_pool.rb:116:in `block in spawn_thread'
2016-12-18T02:32:44.013101+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2016-12-18T02:32:43.362607+00:00 heroku[web.1]: Idling
2016-12-18T02:32:43.362607+00:00 heroku[web.1]: State changed from up to down
2016-12-18T02:32:44.038208+00:00 app[web.1]: - Gracefully stopping, waiting for requests to finish
2016-12-18T02:32:44.038778+00:00 app[web.1]: - Goodbye!
2016-12-18T02:32:44.038774+00:00 app[web.1]: === puma shutdown: 2016-12-18 02:32:44 +0000 ===
2016-12-18T02:32:44.039352+00:00 app[web.1]: Exiting
2016-12-18T02:32:44.259020+00:00 heroku[web.1]: Process exited with status 0
2016-12-18T09:45:45.086929+00:00 heroku[web.1]: State changed from down to starting
2016-12-18T09:45:44.933284+00:00 app[api]: Set SECRET_KEY_BASE config vars by user XXX@gmail.com
2016-12-18T09:45:44.933284+00:00 app[api]: Release v12 created by user XXX@gmail.com

查询B示例:

SELECT 
    issueDate  Date_Time,
    firstName, player_id Player_ID

FROM
 PlayerNote pn join Brand b 

WHERE
    pn.issueDate >= date_format(date_sub(curdate(),interval 1 month),
   '%Y-%m-01') and pn.issueDate < date_format(curdate() ,'%Y-%m-01')

我尝试在case语句中编写这两个查询,例如: 选择案例时(curdate()=&#39; 2016-12-18&#39;)然后(查询A)ELSE(查询B)结束

但收到了 错误代码:1241。操作数应包含1列

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

简单。布尔代数。

SELECT 
    issueDate  Date_Time,
    firstName, player_id Player_ID

FROM
 PlayerNote pn join Brand b 

WHERE
    (curdate()='2016-12-18'
    and pn.issueDate >= date_format(date_sub(curdate(),interval 1 month), '%Y-%m-01') 
    and pn.issueDate < date_format(curdate() ,'%Y-%m-01'))
   or 
   (curdate()<>'2016-12-18'
    and pn.issueDate >= date_format(curdate() ,'%Y-%m-01')
    and pn.issueDate <curdate());

答案 1 :(得分:0)

我不认为你可以使用&#34;选择案例&#34;就像在MySQL中一样。它可以在Oracle中工作(如果我没记错的话。不确定)。据我了解,它只接受单个值(一行一列)

例如,你可以这样做

SELECT CASE 1 WHEN 1 THEN 'one'
WHEN 2 THEN 'two' ELSE 'more' END;

或者

SELECT CASE 1 WHEN 1 THEN (select 1)
WHEN 2 THEN (select 1) ELSE (select 1) END;

但不是这个(返回多行)

SELECT CASE 1 WHEN 1 THEN (select 1 from anytable)
WHEN 2 THEN (select 1 from anytable) ELSE (select 1 from anytable) END;

也不是(返回多列)

SELECT CASE 1 WHEN 1 THEN (select 1,2)
WHEN 2 THEN  (select 1,2) ELSE  (select 1,2) END;

我建议你使用union并在每个子查询中放入数据条件。 例如

SELECT 
    issueDate  Date_Time,
    firstName, player_id Player_ID

FROM
 PlayerNote pn join Brand b 

WHERE
    pn.issueDate >= date_format(date_sub(curdate(),interval 1 month),  '%Y-%m-01') 
     and pn.issueDate < date_format(curdate() ,'%Y-%m-01')
     and curdate()='2016-12-18'

UNION

SELECT 
        issueDate  Date_Time,
        firstName, player_id Player_ID

    FROM
     PlayerNote pn join Brand b 

    WHERE
        pn.issueDate >= date_format(curdate() ,'%Y-%m-01')
        and pn.issueDate <curdate() 
        and curdate()<>'2016-12-18'

我没有测试它,但它应该可以工作。