05店铺列表.sql 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. -- Active: xiaoding_test@@192.168.110.85@3306@xiaoding_test
  2. /*
  3. 您是一位专业的dba,同时会业务分析。请参考以上数据库的设计文档,给我写一个sql,关联查询出:
  4. 项目名称、项目分类、项目价格、项目时长、项目分成、区域管理、下单数量、状态、图片、开启/关闭
  5. */
  6. DROP TABLE IF EXISTS report_shop_list;
  7. CREATE TABLE report_shop_list AS
  8. /* 店铺列表视图 */
  9. DROP VIEW IF EXISTS manage_shop_list;
  10. CREATE OR REPLACE VIEW manage_shop_list AS
  11. WITH
  12. /* 店铺技师数量统计 */
  13. coach_count AS (
  14. SELECT cu.shop_id, COUNT(DISTINCT cu.id) /* 技师总数 */ AS coach_total
  15. FROM coach_users cu
  16. WHERE
  17. cu.deleted_at IS NULL
  18. GROUP BY
  19. cu.shop_id
  20. ),
  21. /* 店铺服务项目数量统计 */
  22. service_count AS (
  23. SELECT ss.shop_id, COUNT(DISTINCT ss.id) /* 服务项目总数 */ AS service_total
  24. FROM shop_services ss
  25. WHERE
  26. ss.deleted_at IS NULL
  27. GROUP BY
  28. ss.shop_id
  29. )
  30. SELECT
  31. si.id as shop_id /* 店铺编号 */,
  32. sar.shop_name as shop_name /* 店铺名称 */,
  33. sar.contact_name as contact_name /* 店铺负责人 */,
  34. sar.contact_phone as contact_phone /* 店铺电话 */,
  35. sar.shop_address as shop_address /* 店铺位置 */,
  36. sar.longitude as longitude /* 经度 */,
  37. sar.latitude as latitude /* 纬度 */,
  38. sar.operating_area as operating_area /* 面积 */,
  39. sar.business_hours as business_hours /* 营业时间 */,
  40. sar.rating as rating /* 店铺星级 */,
  41. sar.storefront_photo as storefront_photo /* 店铺门头照 */,
  42. sar.storefront_front_photo as storefront_front_photo /* 店铺门头照正面 */,
  43. sar.storefront_back_photo as storefront_back_photo /* 店铺门头照背面 */,
  44. sar.state as state /* 认证状态 */,
  45. COALESCE(cc.coach_total, 0) as coach_total /* 技师总数 */,
  46. COALESCE(sc.service_total, 0) as service_total /* 服务项目总数 */
  47. FROM
  48. shop_infos si
  49. LEFT JOIN shop_auth_records sar ON si.auth_record_id = sar.id
  50. AND sar.deleted_at IS NULL
  51. LEFT JOIN coach_count cc ON si.id = cc.shop_id
  52. LEFT JOIN service_count sc ON si.id = sc.shop_id
  53. WHERE
  54. si.deleted_at IS NULL
  55. ORDER BY si.id DESC;