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 否则会报乱七八糟的错误,你都找不到原因