Django 数据库模板的写法

Django 对各种数据库提供了很好的支持,包括:PostgreSQL、MySQL、SQLite、Oracle。

什么是Django呢?

是数据库在where条件判断后面的if else 或for 条件判断使用的,一条SQL语句在from前面可以使用case when来进行判断,在form后面可以使用Django来进行判断。

下面来点例子吧:

 select 
    ta.id ,
    case when ta.account_status=1 then '有效' else '无效' end status_desc,
    case
      when ta.account_type = 1 then '中心'
      when ta.account_type = 2 then '供应商'
      when ta.account_type = 4 then '仓库'
      when ta.account_type = 5 then '客户'
      when ta.account_type = 6 then '代理商'
    else '其他' end type_desc
 from t_account ta
    INNER JOIN t_acct_role tar ON ta.id = tar.account_id  and tar.status=1
    INNER JOIN t_role  tr ON  tr.role_code!=?admin_role_code and tar.role_id=tr.id
    where 1 = 1 and ta.account_status != 3
    {% if account_type == 5 %}
    and ta.account_type=?account_type and ta.create_account_id = ?account_id
    {% endif%}
    {% if account_type == 1 %}
    and (ta.account_type=5 or ta.account_type=1 or ta.account_type=4 or 
    ta.account_type=6)
    {% endif%}
    {% if account_type == 2 || account_type == 4 %}
    and ta.account_type=?account_type
    {% endif%}
    {% if account_type == 2 %}
    and ta.supplier_id=?supplier_id
    {% endif%}
    {% if account_type == 4 %}
     and ta.warehouse_id=warehouse_id
     and ta.warehouse_id in (select warehouse_id from t_account_child_warehouse where 
     account_id = ?account_id)
    {% endif%}
    {% if role_id != "" %}
    and ta.id in (select account_id from t_acct_role where role_id =?role_id and 
    status=1)
    {% endif%}
    {% if status > 0 %}
    and ta.account_status =?status
    {% endif%}
    {% if keyword!="" %}
    and (ta.name ilike '%'||?keyword||'%' or ta.login_name ilike '%'||?keyword||'%' or
    CnFirstChar(ta.name) ilike '%'||?keyword||'%' or CnFirstChar(ta.login_name) ilike 
    '%'||?keyword||'%'
    )
    {% endif%}
    ORDER BY cast(ta.id as integer) DESC

 

 

and是两者都成立            or 有一个条件成立就行 




以上是用的if 判断  现在再讲 for 的用法



现在我认为如果不想在代码中进行循环遍历,可以在sql中用Django中的for来进行遍历循环,

‘0’ 是给的一个默认的值         agent_child_ids 是个[ ]数组 

 

# if标签,支持嵌套,判断条件符必须与变量之间使用空格隔开,否则程序会抛出异常
# {% endif %}与{% endfor %}作用是相同的
{% if name = 'Lily' %}
{{name}}
{% elif name = 'Lucy' %}
{{name}}
{% else %}
{{name}}
{% endif %}
特别注意else if 应该使用elif 否则会报乱七八糟的错误,你都找不到原因

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值