cnblogs/dcrenl/mysql 查询结果为空时值时执行后面的sql语句.html

20 lines
2.5 KiB
HTML
Raw Normal View History

2024-09-24 12:43:01 +08:00
<p>sql server支持变量所以一般使用方法如下</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">DECLARE</span> <span style="color: #008000;">@Val</span> <span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">50</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">select</span> <span style="color: #008000;">@Val</span> <span style="color: #808080;">=</span> param_value <span style="color: #0000ff;">where</span> t_param <span style="color: #0000ff;">where</span> param_name <span style="color: #808080;">=</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">log_type</span><span style="color: #ff0000;">'</span>
<span style="color: #0000ff;">if</span>(<span style="color: #008000;">@Val</span> <span style="color: #0000ff;">is</span> <span style="color: #0000ff;">null</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">begin</span>
<span style="color: #0000ff;">select</span> <span style="color: #008000;">@Val</span> <span style="color: #808080;">=</span> param_default_value <span style="color: #0000ff;">where</span> t_param_info <span style="color: #0000ff;">where</span> param_name <span style="color: #808080;">=</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">log_type</span><span style="color: #ff0000;">'</span>
<span style="color: #0000ff;">end</span>
<span style="color: #0000ff;">select</span> <span style="color: #008000;">@Val</span></pre>
</div>
<p>&nbsp;</p>
<p>mysql中执行语句时不支持定义变量及条件判断所以想了个办法也能达到相同目的</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">SELECT</span><span style="color: #000000;"> IFNULL(
(</span><span style="color: #0000ff;">select</span> param_value <span style="color: #0000ff;">from</span> t_param <span style="color: #0000ff;">where</span> param_name <span style="color: #808080;">=</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">log_type</span><span style="color: #ff0000;">'</span><span style="color: #000000;">),
(</span><span style="color: #0000ff;">select</span> param_default_value <span style="color: #0000ff;">from</span> t_param_info <span style="color: #0000ff;">where</span> param_name <span style="color: #808080;">=</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">log_type</span><span style="color: #ff0000;">'</span><span style="color: #000000;">)
) </span><span style="color: #0000ff;">as</span> param_value;</pre>
</div>
<p>&nbsp;</p>