首页 > 解决方案 > 获取订阅日期的 SQL 查询

问题描述

我有这些表,我想在其中检查用户订阅期:

在此处输入图像描述

实体:

 CREATE TABLE `orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `city` varchar(30) DEFAULT NULL,
  `CONTENT` text DEFAULT NULL,
  `country` varchar(50) DEFAULT NULL,
  `created_at` datetime(6) DEFAULT NULL,
  `discount` decimal(8,2) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `first_name` varchar(50) DEFAULT NULL,
  `grand_total` decimal(8,2) DEFAULT NULL,
  `item_discount` decimal(8,2) DEFAULT NULL,
  `last_name` varchar(50) DEFAULT NULL,
  `line1` varchar(50) DEFAULT NULL,
  `line2` varchar(50) DEFAULT NULL,
  `middle_name` varchar(50) DEFAULT NULL,
  `mobile` varchar(15) DEFAULT NULL,
  `promo` varchar(100) DEFAULT NULL,
  `province` varchar(50) DEFAULT NULL,
  `session_id` int(11) DEFAULT NULL,
  `shipping` decimal(8,2) DEFAULT NULL,
  `status` varchar(100) DEFAULT NULL,
  `sub_total` decimal(8,2) DEFAULT NULL,
  `tax` decimal(8,2) DEFAULT NULL,
  `token` varchar(100) DEFAULT NULL,
  `total` decimal(8,2) DEFAULT NULL,
  `updated_at` datetime(6) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  `phone` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=65 DEFAULT CHARSET=latin1
    
CREATE TABLE `subscription` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `amount` decimal(19,2) DEFAULT NULL,
  `created_at` datetime(6) DEFAULT NULL,
  `currency` varchar(20) DEFAULT NULL,
  `duration` bigint(20) DEFAULT NULL,
  `end_at` datetime(6) DEFAULT NULL,
  `error` varchar(200) DEFAULT NULL,
  `order_id` int(11) DEFAULT NULL,
  `product` varchar(20) DEFAULT NULL,
  `run_at` datetime(6) DEFAULT NULL,
  `start_at` datetime(6) DEFAULT NULL,
  `status` varchar(20) DEFAULT NULL,
  `updated_at` datetime(6) DEFAULT NULL,
  `title` varchar(20) DEFAULT NULL,
  `parent_transaction_id` int(11) DEFAULT NULL,
  `parent_transactionId` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23443556 DEFAULT CHARSET=latin1
    
CREATE TABLE `payment_transactions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `amount` decimal(19,2) DEFAULT NULL,
  `code` varchar(100) DEFAULT NULL,
  `CONTENT` text DEFAULT NULL,
  `created_at` datetime(6) DEFAULT NULL,
  `currency` varchar(20) DEFAULT NULL,
  `error` varchar(200) DEFAULT NULL,
  `external_id` varchar(255) DEFAULT NULL,
  `gateway` varchar(20) DEFAULT NULL,
  `mode` int(11) DEFAULT NULL,
  `order_id` int(11) DEFAULT NULL,
  `reconciled_at` datetime(6) DEFAULT NULL,
  `reference_transaction_id` int(11) DEFAULT NULL,
  `status` varchar(20) DEFAULT NULL,
  `type` varchar(20) DEFAULT NULL,
  `unique_transactionId` varchar(50) DEFAULT NULL,
  `updated_at` datetime(6) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  `unique_transaction_id` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=119 DEFAULT CHARSET=latin1

我是数据库表设计的新手,所以我需要问我应该使用什么表关系关系来使用用户 ID 获取用户订阅期?

使用 Orders 表作为主表并使用 JOIN 进行 SQL 查询以使用用户 id 获取订阅期是一个好主意吗?

JSFFiddle:http ://sqlfiddle.com/#!9/262df6/1

标签: sqlhibernatejoin

解决方案


根据您的要求,您需要使用用户 ID 获取用户订阅期。

您可以通过加入 ORDERS 表从 SUBSCRIPTION 表中获取用户订阅期和其他数据,因为它们具有 order_id,您可以通过在 where 条件下查询 user_id 来直接加入两个表。

select subs.* from subscription subs
left join orders odr on odr.id = subs.order_id
where odr.user_id = 221 -- pass the required user id

推荐阅读