【问题标题】:SQL query for getting subscription dates获取订阅日期的 SQL 查询
【发布时间】:2021-09-07 12:20:58
【问题描述】:

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

实体:

 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

【问题讨论】:

  • 请与示例数据插入脚本共享创建表脚本,并输出您对示例数据的期望。

标签: sql hibernate join


【解决方案1】:

根据您的要求,您需要使用用户 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
  • 指定所需的列并在 where 条件下查询用户 ID
  • 通过在订阅表中添加记录与您共享的 JSFiddle 进行了验证:http://sqlfiddle.com/#!9/b834fc/2

【讨论】:

  • left join 应该只是 join 没有理由进行外连接,无论如何,您在 where 子句中对该外连接表有条件(这会撤消外连接)
【解决方案2】:

这里要考虑的主要问题是您将来通常希望如何访问这些表。根据您发布的内容,我将假设有用户(我没有看到用户表),并且他们下订单并且也有订阅。

它们的共同点——两者之间的关系——是用户。让用户成为焦点也会创建一对多的关系,而不是多对多的关系,后者更容易管理。因此,与其将订单与订阅相关联,这可能会令人困惑,我建议将它们都与用户相关联。如果单个用户有多个订单和多个订阅怎么办?您会将哪个订单与哪个订阅相关联?

如果您有一个 users 表,那么我会对其进行结构化,以便每个用户都有自己唯一的主键。我们称它为 user_id。然后,在订阅(将订阅与用户关联)和订单(将订单与用户关联)中都有 user_id。

通过 user_id 关联两者,您可以轻松关联订单和订阅。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多