【问题标题】:Select subscriptions based on user email根据用户电子邮件选择订阅
【发布时间】:2021-09-09 17:28:54
【问题描述】:

我有这些 MySQL 表,我想在其中存储用户订阅:

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;


INSERT INTO subscription (`id`, `amount`, `created_at`, `currency`, `duration`, `end_at`, `error`, `order_id`, `product`, `run_at`, `start_at`, `status`, `updated_at`, `title`, `parent_transaction_id`, `parent_transactionId`) VALUES 
('122', '3333.00', '2020-10-31 19:41:16.622386', 'USD', '1000', '2020-10-31 19:41:16.622386', 'error message', '122', 'error message', '2020-10-31 19:41:16.622386', '2020-10-31 19:41:16.622386', 'active', '2020-10-31 19:41:16.622386', 'title', '443', '5544');

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(255) DEFAULT NULL,
  `enabled` tinyint(4) DEFAULT NULL,
  `encrypted_password` varchar(255) DEFAULT NULL,
  `first_name` varchar(255) DEFAULT NULL,
  `last_name` varchar(255) DEFAULT NULL,
  `role` varchar(25) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=153 DEFAULT CHARSET=latin1;

INSERT INTO `users` (`id`, `email`, `enabled`, `encrypted_password`, `first_name`, `last_name`, `role`) VALUES 
('192', 'test@mail.com', '1', '$2a$31$ovShEKleI3Yk2vgjAIF5mO4HkGWXcTSKPMTaj7rFN4KAtlEz0f/ay', 'test', 'test', 'ROLE_CLIENT');

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;

INSERT INTO `orders` (`id`, `city`, `CONTENT`, `country`, `created_at`, `email`, `first_name`, `grand_total`, `item_discount`, `last_name`, `status`, `sub_total`, `tax`, `total`, `user_id`) VALUES 
('122', 'city', 'test context', 'USA', '2021-05-25 12:40:23.793779', 'test@email.com', 'first name', '100', '10', 'last name', 'active', '20', '10', '200', '192');
INSERT INTO `orders` (`id`, `city`, `CONTENT`, `country`, `created_at`, `email`, `first_name`, `grand_total`, `item_discount`, `last_name`, `status`, `sub_total`, `tax`, `total`, `user_id`) VALUES 
('124', 'city', 'test context', 'USA', '2021-05-25 12:40:23.793779', 'test@email.com', 'first name', '100', '10', 'last name', 'active', '20', '10', '200', '192');


CREATE TABLE `order_item` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `discount` decimal(19,2) DEFAULT NULL,
  `order_id` int(11) DEFAULT NULL,
  `price` decimal(19,2) DEFAULT NULL,
  `product_id` int(11) DEFAULT NULL,
  `quantity` int(11) DEFAULT NULL,
  `sku` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

SQLFiddle:http://sqlfiddle.com/#!9/1d6ba6/34

我使用这个带有 2 个 JOINS 的 SQL 查询来获取订阅:

SELECT *
FROM subscription s
INNER JOIN Orders o  ON s.order_id = o.id AND s.id = 122
INNER JOIN users u ON u.id = o.user_id
ORDER BY s.`created_at` 
LIMIT 1; 

如您所见,我使用参数 id 来进行 2 次联接。是否可以仅将用户表中的用户电子邮件作为参数发送并获取找到的订阅记录?

【问题讨论】:

  • 您在orders 表中有一个email 字段,因此您可以使用它。但你一开始就不应该有这样的冗余数据。如果用户更改了他们的电子邮件,您将必须更新他们的所有订单。
  • 这就是问题所在。我想使用表Users 中的email 字段。
  • 那么问题出在哪里?将WHERE u.email = 'user@domain.com' 添加到您的查询中。
  • 是否可以以某种方式编辑查询以便我可以在这里使用它:ON s.order_id = o.id AND s.id = 122

标签: mysql sql mariadb


【解决方案1】:

单个表的条件应该放在WHERE 子句中,而不是ON

SELECT *
FROM subscription s
INNER JOIN Orders o  ON s.order_id = o.id
INNER JOIN users u ON u.id = o.user_id
WHERE u.email = 'user@example.com'
ORDER BY s.`created_at` 
LIMIT 1; 

【讨论】:

  • 我可以删除s.id = 122 对吗?我不需要按 id 搜索。
猜你喜欢
  • 1970-01-01
  • 2019-12-08
  • 1970-01-01
  • 2023-03-12
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
  • 2014-04-03
  • 1970-01-01
相关资源
最近更新 更多