thinkphp中if使用以及concat的使用实例
thinkphp中联合if和concat的使用方法
"IF (pp.reason_name != '',concat( pp.reason_name, if(pp.remark != '',',',''), pp.remark ),'' ) " => 'remark'
if使用方法 IF(expr1,expr2,expr3)
如果 expr1 是TRUE (expr1 <> 0 and expr1 <> NULL),则 IF()的返回值为expr2; 否则返回值则为 expr3。IF()的返回值为数字值或字符串值,具体情况视其所在语境而定。
"IF (pp.reason_name != '','显示表达式' ,'')" => '使用别名'
concat使用方法
CONCAT()函数用于将多个字符串连接成一个字符串
例子
mysql> SELECT CONCAT(’My’, ‘S’, ‘QL’);
-> ‘MySQL’
concat( pp.reason_name, if(pp.remark != '',',',''), pp.remark )
查询sql语句
Db::name('spu_info')->alias('pi') ->leftJoin('spu p', 'p.spu = pi.spu') ->leftJoin('sku k', 'k.spu = pi.spu') ->leftJoin('supplier s', 's.id = k.supplier_id') ->leftJoin('product_process pp', 'pp.id = p.process_id and pp.spu = p.spu') ->LeftJoin('spu_sub ss', 'ss.spu = pi.spu')
->field([ "IF (pp.reason_name != '',concat( pp.reason_name, if(pp.remark != '',',',''), pp.remark ),'' ) " => 'remark', 'pi.count', 'pi.is_copy'])->group('pi.spu') ->paginate( $pagesize, false, ['query' => Request::param()] )->toArray();
连接if 与concat使用实例
case is_stop when '1' then '停用' else '在用' end is_stop
Db::name("dictionary") ->field("id,name,name_en,is_stop status,case is_stop when '1' then '停用' else '在用' end is_stop,create_at,memo,num") ->where("pid", 0) // ->buildSql(); ->select() ->toArray();
case 与 when 联合使用
判断年龄天数
# when 表达式中可以使用 and 连接条件 SELECT NAME '英雄', age '年龄', CASE WHEN age < 18 THEN '少年' WHEN age < 30 THEN '青年' WHEN age >= 30 AND age < 50 THEN '中年' ELSE '老年' END '状态' FROM user_info;
if 查询sql
SELECT DISTINCT `pa`.`email` AS `paypal邮箱`, `ptl`.`transaction_updated_date` AS `时间日期`, `ptl`.`transaction_id` AS `交易号`, `ptl`.`paypal_reference_id` AS `参考交易号`, `ptl`.`transaction_event_code` AS `类型`, `ptl`.`transaction_status` AS `状态`, `ptl`.`currency_code` AS `币种`, ( ptl.transaction_amount + ptl.fee_amount ) AS `总额`, `ptl`.`transaction_amount` AS `费用`, `ptl`.`fee_amount` AS `净值`, `pa`.`email` AS `收件人邮箱`, `ptl`.`transaction_subject` AS `物品名称`, `ptl`.`available_balance` AS `余额`, IF ( ptl.transaction_amount > 0 , '贷记', '借记') AS `引起余额变动` FROM `paypal_transaction_list` AS `ptl` LEFT JOIN `paypal_transaction_detail` AS `ptd` ON `ptl`.`transaction_id` = `ptd`.`transaction_id` LEFT JOIN `paypal_account` AS `pa` ON `pa`.`paypal_account` = `ptl`.`account_number` WHERE `pa`.`id` IN ( 109 ) AND `ptl`.`transaction_updated_date` BETWEEN '2021-01-01 00:00:00' AND '2021-02-01 00:00:00' ORDER BY `ptl`.`id` ASC, `ptl`.`transaction_updated_date` ASC
case when使用实例
SELECT DISTINCT `pa`.`email` AS `paypal邮箱`, `ptl`.`transaction_updated_date` AS `时间日期`, `ptl`.`transaction_id` AS `交易号`, `ptl`.`paypal_reference_id` AS `参考交易号`, `ptl`.`transaction_event_code` AS `类型`, case `ptl`.`transaction_status` when 'D' then '拒绝交易' when 'F' then '部分退款' when 'P' then '待付款' when 'S' then '交易完成' when 'V' then '退全款' else '未知状态' end AS `状态`, `ptl`.`currency_code` AS `币种`, ( ptl.transaction_amount + ptl.fee_amount ) AS `总额`, `ptl`.`transaction_amount` AS `费用`, `ptl`.`fee_amount` AS `净值`, `pa`.`email` AS `收件人邮箱`, `ptl`.`transaction_subject` AS `物品名称`, `ptl`.`available_balance` AS `余额`, IF ( ptl.transaction_amount > 0 , '贷记', '借记') AS `引起余额变动` FROM `paypal_transaction_list` AS `ptl` LEFT JOIN `paypal_transaction_detail` AS `ptd` ON `ptl`.`transaction_id` = `ptd`.`transaction_id` LEFT JOIN `paypal_account` AS `pa` ON `pa`.`paypal_account` = `ptl`.`account_number` WHERE -- -- `pa`.`id` IN ( 109 ) -- AND `ptl`.`transaction_updated_date` BETWEEN '2021-01-01 00:00:00' AND '2021-02-01 00:00:00' ORDER BY `ptl`.`id` ASC, `ptl`.`transaction_updated_date` ASC