数据库if语句怎么写
- 综合知识
- 关注:1.05W次
如果是 SQL 语句里面使用。
使用 DECODE 或者 CASE WHEN
DECODE 的用法就是
DECODE(abc, 50, 'A', 'B')
CASE WHEN 的用法就是
CASE WHEN abc=50 THEN 'A' ELSE 'B' END
如果是在存储过程里面的话,就是简单的 IF/ELSE 了
SQL 语句的话,就是
SELECT
abc, DECODE(abc, 50, 'A', 'B')
FROM
表
2. SQL中if语句判断时间怎么写1. 建议这种逻辑在代码中写,不要增加数据库负担
2. 非要写在sql里面可以用case when:
case when (tm > '7:00' and tm < '10:30') then "早上" else case when (tm > '10:30' and tm < '15:00') then "中午" else case when(tm > '17:00' and tm < '21:00') then "晚上" end end end
3. php用 if语句 如何输出数据库内容先定义头部信息,表示输出一个excel。
然后再以table的形式把数据库的信息循环的echo出来。
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=xls_region.xls");
$cfg_dbhost = 'localhost';
$cfg_dbname = 'testdb';
$cfg_dbuser = 'root';
$cfg_dbpwd = 'root';
$cfg_db_language = 'utf8';
// END 配置
//链接数据库
$link = mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);
mysql_select_db($cfg_dbname);
//选择编码
mysql_query("set names ".$cfg_db_language);
//users表
$sql = "desc users";
$res = mysql_query($sql);
echo "";
//导出表头(也就是表中拥有的字段)
while($row = mysql_fetch_array($res)){
$t_field[] = $row['Field']; //Field中的F要大写,否则没有结果
echo "".$row['Field']."";
}
echo "";
//导出100条数据
$sql = "select * from users limit 100";
$res = mysql_query($sql);
while($row = mysql_fetch_array($res)){
echo "";
foreach($t_field as $f_key){
echo "".$row[$f_key]."";
}
echo "";
}
echo "";
?>
4. IF语句怎么写create table 仓库表
(
仓库号 int not null,
数量 int not null
)
select * from 仓库表
insert 仓库表 values (12,0)
insert 仓库表 values (13,0)
//我要以仓库号赋值取数据,如果是仓库号为A,则把a,c取出来,如果是仓库号为B,
//则只取出b.即仓库号为A的把所有数据都取出来,B则只要大于0的数据.sql应该怎么写呢?
declare @number int,@value int
set @number=0
set @value = 12
select @number=仓库号 from 仓库表 where 仓库号= @value
print '所查询出的记录号为:'+Convert(varchar,@number)
if(@number=13)
select * from 仓库表 where 仓库号=@number
else if (@number=12)
select 仓库号 from 仓库表 where 仓库号=@number
else
print '此次查询没有查到记录'
以上已经帮你测试好了,拿过来就可直接用了
- 文章版权属于文章作者所有,转载请注明 https://dongkepu.com/zonghezhishi/jpowpv.html