Код: Выделить всё
$userid = $_POST['userid'];
$videoid = $_POST['videoid'];
$subscribedquery = $this->db->query("
select id
from usersubscription
where plan_id IN (
SELECT DISTINCT plan_id
FROM subscribed_videos sv
where sv.videoid = $videoid
)
OR id IN (
SELECT DISTINCT assosiated_plan_id
FROM subscription_groups sg
JOIN subscribed_videos sv ON sv.plan_id = sg.plan_id
WHERE sv.videoid = $videoid
)
and user_id=$userid
");
Код: Выделить всё
CREATE TABLE IF NOT EXISTS `subscribed_videos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`plan_id` int(11) NOT NULL,
`videoid` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;
INSERT INTO `subscribed_videos` (`id`, `plan_id`, `videoid`) VALUES
(7, 2, 1),
(8, 2, 2),
(14, 1, 3),
(15, 1, 4),
(16, 1, 5),
(17, 1, 21),
(18, 1, 28),
(19, 1, 2),
(20, 3, 4),
(21, 3, 6),
(24, 5, 25),
(25, 6, 5);
CREATE TABLE IF NOT EXISTS `subscription_groups` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`plan_id` int(11) NOT NULL,
`assosiated_plan_id` int(11) NOT NULL,
`added_on` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `subscription_groups` (`id`, `plan_id`, `assosiated_plan_id`, `added_on`) VALUES
(1, 1, 1, 0),
(2, 2, 2, 0),
(3, 3, 3, 0),
(4, 4, 1, 0),
(5, 4, 2, 0),
(6, 4, 3, 0),
(12, 5, 5, 0),
(13, 5, 1, 0),
(14, 5, 2, 0),
(15, 6, 1, 0);
CREATE TABLE IF NOT EXISTS `subscription_plans` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`plan` varchar(256) NOT NULL,
`days_limit` int(11) NOT NULL,
`added_on` int(11) NOT NULL,
`status` int(11) NOT NULL,
`rate` decimal(6,2) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `subscription_plans` (`id`, `plan`, `days_limit`, `added_on`, `status`, `rate`) VALUES
(1, 'PlanA', 15, 1398249706, 1, 150.00),
(2, 'PlanB', 15, 1398249679, 1, 100.00),
(3, 'PlanC', 15, 1398249747, 1, 100.00),
(4, 'PlanD', 10, 1398249771, 1, 500.00),
(5, 'PlanE', 15, 1398250104, 1, 200.00),
(6, 'Plan R1', 20, 1398250104, 1, 200.00);
CREATE TABLE IF NOT EXISTS `usersubscription` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`plan_id` int(11) NOT NULL,
`subscribed_on` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `usersubscription` (`id`, `user_id`, `plan_id`, `subscribed_on`) VALUES
(1, 1, 1, 1399091458);
Код: Выделить всё
SELECT * FROM subscribed_videos;
+----+---------+---------+
| id | plan_id | videoid |
+----+---------+---------+
| 7 | 2 | 1 |
| 8 | 2 | 2 |
| 14 | 1 | 3 |
| 15 | 1 | 4 |
| 16 | 1 | 5 |
| 17 | 1 | 21 |
| 18 | 1 | 28 |
| 19 | 1 | 2 |
| 20 | 3 | 4 |
| 21 | 3 | 6 |
| 24 | 5 | 25 |
| 25 | 6 | 5 |
+----+---------+---------+
SELECT * FROM subscription_groups;
+----+---------+--------------------+----------+
| id | plan_id | assosiated_plan_id | added_on |
+----+---------+--------------------+----------+
| 1 | 1 | 1 | 0 |
| 2 | 2 | 2 | 0 |
| 3 | 3 | 3 | 0 |
| 4 | 4 | 1 | 0 |
| 5 | 4 | 2 | 0 |
| 6 | 4 | 3 | 0 |
| 12 | 5 | 5 | 0 |
| 13 | 5 | 1 | 0 |
| 14 | 5 | 2 | 0 |
| 15 | 6 | 1 | 0 |
+----+---------+--------------------+----------+
SELECT * FROM subscription_plans;
+----+---------+------------+------------+--------+--------+
| id | plan | days_limit | added_on | status | rate |
+----+---------+------------+------------+--------+--------+
| 1 | PlanA | 15 | 1398249706 | 1 | 150.00 |
| 2 | PlanB | 15 | 1398249679 | 1 | 100.00 |
| 3 | PlanC | 15 | 1398249747 | 1 | 100.00 |
| 4 | PlanD | 10 | 1398249771 | 1 | 500.00 |
| 5 | PlanE | 15 | 1398250104 | 1 | 200.00 |
| 6 | Plan R1 | 20 | 1398250104 | 1 | 200.00 |
+----+---------+------------+------------+--------+--------+
SELECT * FROM usersubscription
+----+---------+---------+---------------+
| id | user_id | plan_id | subscribed_on |
+----+---------+---------+---------------+
| 1 | 1 | 1 | 1399091458 |
+----+---------+---------+---------------+
Я ожидаю, что результат будет таким, если пользователь уже подписан на план выбранного видео, в противном случае запрос должен возвращать пустые записи:
Код: Выделить всё
id
---
1
Может ли кто-нибудь помочь мне найти подходящее решение для этой проблемы?
Подробнее здесь: https://stackoverflow.com/questions/234 ... have-quali