mysql 一個字段存多個id,關聯查詢

mysql訂單表中一個字段對應多個優惠卷id,用','隔開。html

財務須要優惠卷使用狀況mysql

剛開始用優惠卷id(長度固定,不會出現特殊狀況) like 訂單表關聯id字段 查詢,使用'||'拼接'%'。  sql

發現不管是在優惠卷id或訂單管理字段加'%'都不能打到要求,最後使用find_in_set()方法 成功實現。spa

代碼以下:code

select cou.cp_id as '優惠卷id'htm

,cou.cp_no as '卷號'get

,cast(case cou.generate_type  when 0 then '生成'  when 1 then '發放' else cou.generate_type string

end AS char(20)) as '生成類型'it

,cast(case cou.state when 0 then '待領用' when 1 then '未使用' when 2 then '已使用' when 3 then '已過時' when 4 then '已失效' else cou.state end as char(20)) as '狀態'io

,cast(case cou.is_gift when 0 then '否' when 1 then '是' else '' end as char(20))as '是否禮包領取'

,cast(case cou.cp_type when 1 then '現金券' when 2 then '折扣券' when 3 then '滿減券' when 4 then '買贈券' when 5 then '福利券' when 6 then '禮包券' else cou.cp_type end as char(20)) as '類型'

,cast(case cou.pro_property when 0 then '社區通用' when 1 then '商家專用' when 2 then '內容通用' when 3 then '禮包發放' else cou.pro_property end as char(20)) as '卷屬性'

,cou.start_date as '開始時間'

,cou.end_date as '結束時間'

,cou.discount as '折扣'

,cou.given_number as '贈送數量'

,cou.buy_number as '購買數量'

,cast(case cou.is_limit when 0 then '否' when 1 then '是' else cou.is_limit end as char(20) )as '是否限額'

,cou.preferential_price as '優惠金額'

,cou.seller_id as '商家id'

,crm.seller_name as '商家'

,cou.user_id as '業戶id'

,oinfo.owner_name as '業戶'

,pro.pro_title as '活動標題'

,cast(case pro.cp_type when 1 then '現金券' when 2 then '折扣券' when 3 then '滿減券' when 4 then '買贈券' when 5 then '福利券' else pro.cp_type end as char(20)) as '活動類型'

,o.create_userid as '訂單用戶id'

,u.owner_name as '訂單用戶'

,o.create_date as '下單時間'

,case o.module_type when 1 then '鄰聚街' when 2 then '精選商品' when 3 then '限時購' when 4 then '團購' when 6 then '到家服務' when 7 then '普通商品' end as '訂單類型'

,o.total_money as '訂單金額'

,c.seller_name as '商家名稱'

,case o.order_status when 3 then '已完成' when 9 then '已關閉' when 7 then '待收貨' when 1 then '待處理' when 2 then '處理中' when 4 then '已取消' when 5 then '待付款' when 6 then '待發貨' when 8 then '待收款' end as '訂單狀態'

,case t.as_state when 1 then '申請中' when 2 then '已拒絕' when 3 then '已經過' when 4 then '已撤銷' when 5 then '已完成' end as '退款狀態'

,t.refund_amount as '退款金額'

,o.coupons_id as '優惠卷id'

,o.coupons_money as '優惠卷金額'

from  (tbc_coupons cou,tbg_order_record o) 

left join  tbo_owner_info_new oinfo on cou.user_id=oinfo.ownerinfo_id

left join crm_manage_seller crm on cou.seller_id=crm.seller_id 

left join tbc_promotion pro on cou.pro_id=pro.pro_id

left JOIN tbo_owner_info_new u on o.create_userid= u.ownerinfo_id 

 left JOIN crm_manage_seller c on o.seller_id=c.seller_id 

 left JOIN tbg_after_sale t on  o.as_id=t.as_id

where cou.pro_id is not null 

and cou.end_date>'2015-12-1'  

and find_in_set(cou.cp_id,o.coupons_id) 

and o.operate_date>= '2015-12-1' 

and o.operate_date< '2015-12-30' 

order by cou.seller_id,cou.cp_type

核心代碼:

select * from a,b where find_in_set(a.id,b.aids);

find_in_set(str,strlist)方法返回查詢查詢到的str在strlist的位置(從1開始),爲找到返回0.若是分割符不爲','可用 REPLACE(str,from_str,to_str)替換

參考:

http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set

相關文章
相關標籤/搜索