Mysql左連線分頁查詢

2021-10-08 12:24:47 字數 1864 閱讀 2367

個人**:

1.常見錯誤

select * from user t1 left join user_tages t2 on t2.userid=t1.id  limit 0,10

這種先連線查詢在分頁的是錯誤的,正常的是先分頁,再連線查詢

改進:(使用子查詢)

select t1.id,t1.program_token,t1.coupon_type,t1.name,t1.deduct,t1.discount,t1.valid_for_all_shop,

t1.lower_consumer_amount,t2.value_id as t_id,t2.item_type as t_type,

case

when t2.item_type='card' then (select card_name from card where id=t2.value_id and valid=1 and

program_token=#)

when t2.item_type='service' then (select title from ord_service where id=t2.value_id and valid=1 and

program_token=#)

endas t_name,

t1.valid_start_date,t1.valid_end_date,

t1.amount,t1.remain_amount,t1.use_range,

(select count(user_info_id) from user_coupons where coupon_id=t1.id) as received_amount,

(select count(user_info_id) from user_coupons where coupon_id=t1.id and use_status=1) as used_amount,

(select count(shop_id) from coupon_shop_relation where coupon_id=t1.id and valid=1) as able_shop_amount,

case

when t1.stop_grant_status=1 or now() > t1.valid_end_date then '已結束'

when now() between t1.valid_start_date and t1.valid_end_date then '進行中'

else '未開始'

endas status,

case

when t1.coupon_type='discount' then '折扣券'

when t1.coupon_type='deduction' then '抵扣券'

else '未知'

endas coupon_type_name,

case

when t1.use_range='all' then '所有專案'

when t1.use_range='part' then '指定專案'

else '未知'

endas use_range_name

from

(select * from coupon t where t.program_token=# and t.valid=1

and t.name like #

and t.coupon_type=#

order by t.create_time desc limit

#,# ) t1

left join coupon_item_relation t2 on t1.id=t2.coupon_id

order by t1.create_time desc

Mysql內連線 左連線 右連線查詢

建表語句如下 建立表a table table structure for a table drop table if exists a table create table a table id smallint 6 auto increment,name varchar 10 not null,...

JPA 多表左連線多條件分頁查詢

業務場景 主表 訂單表,與客戶表 產品表 物流表存在一對一關係,對映欄位為id,現需要根據訂單編號 訂單日期 客戶名稱 客戶編號 產品名稱 產品編號 快遞單號查詢該筆訂單,需要支援模糊查詢和分頁。order實體類中的需要進行一對一關係對映 onetoone joincolumn name expre...

mysql 左連線 重複 mysql左連線重複行

我正在設計乙個類似於4個 的字典資料庫 words wordid lemma senses wordid synsetid synsets synsetid definition samples synsetid sample所以我使用下面的查詢來獲取所有意義定義和樣本 if isset searc...