sql - 获取订阅日期的 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
解决方案
根据您的要求,您需要使用用户 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
推荐阅读
- php - 使用 php 创建和发送 doc (word) 文件
- spring - 多个 application.yml 未在 Spring Boot 中合并
- c - 为什么在此示例中我会收到带有 argv 的崩溃报告?
- c# - 如何将 C++ std::push_heap 转换为 C#?
- c# - 罗斯林。将程序集引用添加到项目
- typescript - 迁移到 TypeScript 后,Console.log 在 React Native 应用程序中不起作用
- r - R中的一列中的多个日期,缺少时间和NA值
- android - 如何在android中为用户指定的时间设置多个提醒
- ios - 比较相同图像的按钮
- kivy - 在kivy中重置SelectableLabel