我有一个学校的Mysql数据库,每个课程(pj001_courses),有一个或多个相关的课程(任务)。中间有一个连接表(pj001_courses_tasks_c)。
pj001_courses:(id,name)
pj001_courses_tasks_c:(id,pj001_courses_taskspj001_courses_ida,pj001_courses_taskstasks_idb)
任务:(id,name,date_start,date_modified,status)
地点:pj001_courses.id = pj001_courses_tasks_c.pj001_courses_taskspj001_courses_ida 和 tasks.id = pj001_courses_tasks_c。 pj001_courses_taskstasks_idb
我想通过查询获得的是所有课程的列表,其中包含最近的“已完成”课程(状态=“已完成”),其中包括以下字段:
Corso,即 Id corso, idlez, Lezione, DATE_START, DATE_MODIFIED
在下面,您可以找到创建示例mysql测试数据库的代码:
-- phpMyAdmin SQL Dump
-- version 4.0.10deb1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generato il: Apr 07, 2017 alle 08:24
-- Versione del server: 5.5.53-0ubuntu0.14.04.1
-- Versione PHP: 5.5.9-1ubuntu4.20
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `speak_italy_stov`
--
-- --------------------------------------------------------
--
-- Struttura della tabella `pj001_courses`
--
CREATE TABLE IF NOT EXISTS `pj001_courses` (
`id` char(36) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`date_entered` datetime DEFAULT NULL,
`date_modified` datetime DEFAULT NULL,
`modified_user_id` char(36) DEFAULT NULL,
`created_by` char(36) DEFAULT NULL,
`description` text,
`deleted` tinyint(1) DEFAULT '0',
`assigned_user_id` char(36) DEFAULT NULL,
`startdate` date DEFAULT NULL,
`startlevel` varchar(5) DEFAULT NULL,
`time` varchar(5) DEFAULT NULL,
`lessonsday` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
--
-- Dump dei dati per la tabella `pj001_courses`
--
INSERT INTO `pj001_courses` (`id`, `name`, `date_entered`, `date_modified`, `modified_user_id`, `created_by`, `description`, `deleted`, `assigned_user_id`, `startdate`, `startlevel`, `time`, `lessonsday`) VALUES
('3d87bac4-49b0-3acb-3010-56684570afd9', 'Course1', '2015-12-09 15:14:11', '2017-04-06 14:09:28', '1', '1', NULL, 0, '', NULL, NULL, NULL, NULL),
('bb9311ed-8e1b-7715-9222-565f2e684b49', 'Course2', '2015-12-02 17:48:24', '2017-04-06 12:29:58', '1', 'e07880a3-a57d-a20c-9e25-565eb7af093f', NULL, 0, '', '2015-10-14', '80+', '12.45', '^Tuesday^,^Friday^'),
('c608ec6d-960e-eb4f-760d-577b81007df9', 'Course3', '2016-07-05 09:43:30', '2017-04-06 12:31:22', '1', '1', NULL, 0, '', NULL, NULL, NULL, NULL),
('ce7c7bb1-c511-82e5-40bf-56616a7a10cc', 'Course4', '2015-12-04 10:27:39', '2017-04-06 12:35:11', '1', 'c0aa6b17-16d0-baea-c4ae-566162ba3706', NULL, 0, '', '2015-10-05', '10', '10.45', '^Monday^,^Thursday^'),
('eb914486-79d6-07e6-8263-566188671f7a', 'Course5', '2015-12-04 12:33:05', '2016-06-28 13:23:41', '1', 'e07880a3-a57d-a20c-9e25-565eb7af093f', NULL, 0, '', '2015-10-12', 'L.41', '17.00', '^Monday^,^Thursday^'),
('f05b48a8-5a13-5e9f-a823-58e6373e80d7', 'Course6', '2017-04-06 12:41:06', '2017-04-06 12:41:06', '1', '1', NULL, 0, NULL, NULL, '', '', '');
-- --------------------------------------------------------
--
-- Struttura della tabella `pj001_courses_tasks_c`
--
CREATE TABLE IF NOT EXISTS `pj001_courses_tasks_c` (
`id` varchar(36) NOT NULL,
`date_modified` datetime DEFAULT NULL,
`deleted` tinyint(1) DEFAULT '0',
`pj001_courses_taskspj001_courses_ida` varchar(36) DEFAULT NULL,
`pj001_courses_taskstasks_idb` varchar(36) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `pj001_courses_tasks_ida1` (`pj001_courses_taskspj001_courses_ida`),
KEY `pj001_courses_tasks_alt` (`pj001_courses_taskstasks_idb`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
--
-- Dump dei dati per la tabella `pj001_courses_tasks_c`
--
INSERT INTO `pj001_courses_tasks_c` (`id`, `date_modified`, `deleted`, `pj001_courses_taskspj001_courses_ida`, `pj001_courses_taskstasks_idb`) VALUES
('115cbbd6-a061-5ba8-feb4-56756fa6fa6c', '2015-12-19 14:55:56', 0, '3d87bac4-49b0-3acb-3010-56684570afd9', 'fb94089f-57e9-0918-c98f-56756fa424d4'),
('16ed5813-c5d1-f831-9887-56efc618e85a', '2017-04-06 12:18:23', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '15c65e64-83d5-e27e-9ded-56efc6034ec7'),
('17ef5857-b4a6-6dbf-dedd-58cf9a076172', '2017-03-20 09:03:40', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '1756da3f-3356-1dea-96b4-58cf9adab8ea'),
('1d05994a-45fa-0370-5304-58cf9b51f8e1', '2017-03-20 09:04:53', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '1c7f7856-11e6-22a7-786b-58cf9b7d42f4'),
('211b53d7-3bb3-5afe-af27-58cf9ac53d35', '2017-03-20 09:03:40', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '2092dc4e-ad01-34b8-0b80-58cf9a0b0c90'),
('23e767e6-a6c1-304f-92e3-5671a4ec937d', '2017-04-06 12:38:05', 1, 'eb914486-79d6-07e6-8263-566188671f7a', '226c2475-16a3-e0af-efaa-5671a483f0e2'),
('24c80d60-f81d-c32f-a368-56684568c447', '2015-12-09 15:16:30', 0, 'eb914486-79d6-07e6-8263-566188671f7a', '18f60d90-0554-4880-760b-5668455dd961'),
('27228e24-b7ab-a384-8fe6-58cf9bdbcd56', '2017-03-20 09:04:53', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '26af672a-823d-2d07-2146-58cf9be2b6f2'),
('284f980c-9dab-2a7f-3526-57738ad682dc', '2017-04-06 12:18:23', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '2733a707-e734-6a68-c63c-57738a212cd3'),
('2be389fe-1c71-ebde-414e-56616dc0040d', '2015-12-04 10:41:15', 0, 'ce7c7bb1-c511-82e5-40bf-56616a7a10cc', '2996ca74-f2d5-a0bf-0c92-56616d7db8d4'),
('2fdea17a-a236-95b0-586f-58cf9b041a93', '2017-03-20 09:04:53', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '2f72251d-5ca1-9890-1dc7-58cf9b790e86'),
('30afc042-2d41-ad85-1ace-566b214472a2', '2015-12-11 19:16:20', 0, '3d87bac4-49b0-3acb-3010-56684570afd9', '2f19dd24-1c2f-a245-1856-566b214d7abd'),
('386ce887-a266-8ef6-7d07-58cf9b0b74bb', '2017-03-20 09:04:53', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '37f7bc74-46c4-0094-f3ad-58cf9b2f00f6'),
('3ac48770-4ed0-fd25-b87f-56616ec579cf', '2015-12-04 10:45:59', 0, 'ce7c7bb1-c511-82e5-40bf-56616a7a10cc', '39109517-1f24-8048-4fca-56616e12fc48'),
('3e440aab-0536-7521-7ac5-5668440a96d9', '2015-12-09 15:13:02', 0, 'bb9311ed-8e1b-7715-9222-565f2e684b49', '16ce0346-064f-c93e-19ca-5668427c30b4'),
('4ab9d6f9-0e63-a8c4-5b7a-58cfd830770a', '2017-04-06 12:18:23', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '4a1d0654-39fe-1c37-7c0c-58cfd8bd7105'),
('4f02bb09-7576-b8bc-7187-58cf9cee8060', '2017-03-20 09:12:42', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '4e78ed60-24c8-fcf8-7b62-58cf9ccac2b4'),
('4faff722-1eb8-05ec-2ffe-56718c87b046', '2017-04-06 12:37:27', 1, 'eb914486-79d6-07e6-8263-566188671f7a', '4dd39006-f652-3ae1-d6a6-56718c46617e'),
('59d2498f-8931-7716-3e7b-58e64b5ae4c1', '2017-04-06 14:08:03', 0, '3d87bac4-49b0-3acb-3010-56684570afd9', '595a2e58-76bc-c134-3cef-58e64b59e8d5'),
('5c3f2895-e160-04e9-d79e-58cfd8565416', '2017-03-20 13:26:40', 0, '3d87bac4-49b0-3acb-3010-56684570afd9', '5ba36af1-8f2b-075b-926f-58cfd87a83e7'),
('672b6c68-fcc6-3945-7a47-58cfd8b2d040', '2017-04-06 12:18:23', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '66a5b45f-4173-387f-8f3d-58cfd8f91b9d'),
('6944176d-6f2b-3369-1e76-5672a363aa43', '2017-04-06 12:38:27', 1, 'eb914486-79d6-07e6-8263-566188671f7a', '6693cd37-0b74-1d97-c395-5672a343260d'),
('6ce06c21-5db1-a921-a37e-57729c1877d4', '2017-04-06 12:18:23', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '743cf2ca-e983-2f08-a2fb-5772966547a1'),
('6d43042f-8f79-8d06-7d9e-58cf9bed69db', '2017-03-20 09:10:57', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '65730b79-3f6e-4385-c83e-58cf9b3a6cb4'),
('6e8d9e2b-0300-4b67-b639-58cf9cc1d141', '2017-03-20 09:12:42', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '6e1bd69b-6501-95d5-1aa8-58cf9c44c529'),
('70d706d1-ddba-1174-e73a-58cfd83ed1e5', '2017-04-06 12:18:23', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '705d3dad-8f77-04a6-05c5-58cfd8124fd5'),
('7613bd00-0397-5b93-5aa9-57738ba67cca', '2016-06-29 08:47:35', 0, '3d87bac4-49b0-3acb-3010-56684570afd9', '7488d168-758c-f760-ed28-57738b5565db'),
('78107ede-3f85-80f6-bad0-56718c341009', '2015-12-16 16:09:30', 0, 'eb914486-79d6-07e6-8263-566188671f7a', '760f5ee8-6470-9291-6733-56718c949974'),
('790ba77f-4723-e52a-b991-58cf9c9da98c', '2017-03-20 09:12:42', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '788c0fcf-c454-c9fb-3af3-58cf9c82b7d3'),
('81af0547-2f08-61d6-ed81-577b8160db2a', '2016-07-05 09:44:33', 0, 'c608ec6d-960e-eb4f-760d-577b81007df9', '80859612-d91f-bd91-8198-577b81c9dd09'),
('852fc19f-2f48-942f-0ecc-58cf9c5abcc7', '2017-03-20 09:12:42', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '8462c635-7ccc-dc53-d152-58cf9c3d0222'),
('87a6b49d-5943-4a26-7193-566aac243068', '2017-04-06 12:18:23', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '85bd5d08-110b-5bce-f564-566aac8ffa97'),
('8aec1222-0b3e-b509-5801-58e63152154f', '2017-04-06 12:16:49', 0, '3d87bac4-49b0-3acb-3010-56684570afd9', '8a635f3b-9e0f-16aa-5532-58e631257729'),
('936b09a7-3940-e350-6815-56616eeff1c5', '2015-12-04 10:43:35', 0, 'ce7c7bb1-c511-82e5-40bf-56616a7a10cc', '92a4c747-b1b7-2949-bcf6-56616ee3b065'),
('9a2359bb-6fc9-ed44-7c81-58cfb514f537', '2017-03-20 13:26:13', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '999d63e2-67a7-b3a2-b236-58cfb505406b'),
('9b3b1f2b-3e3e-30fe-e9a5-58cf9de1a73b', '2017-03-20 10:57:33', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', '9aac98ea-703d-5aef-391d-58cf9d973ff2'),
('9f230245-53e5-cd01-e775-577b8404ac60', '2016-07-05 09:58:43', 0, 'c608ec6d-960e-eb4f-760d-577b81007df9', '8c8404b6-4e53-5f2b-fcce-577b84f9dceb'),
('9f97d432-9484-5417-7818-58e6352168ff', '2017-04-06 12:34:10', 0, 'ce7c7bb1-c511-82e5-40bf-56616a7a10cc', '9ecdc18e-9187-da7e-54d9-58e635cddf77'),
('a2a1b248-dd8a-a7cd-fca6-56616e6c40c8', '2015-12-04 10:43:35', 0, 'ce7c7bb1-c511-82e5-40bf-56616a7a10cc', 'a2058842-e181-b794-6343-56616ef2e0b2'),
('a680033e-316b-ef9b-0dde-58cf9d9c632a', '2017-03-20 10:57:33', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', 'a5fc3006-fd64-e9dc-fb94-58cf9d5debdc'),
('a81bbdd2-b462-b6d5-55a7-567673ca4ad5', '2015-12-20 09:25:15', 0, 'ce7c7bb1-c511-82e5-40bf-56616a7a10cc', 'a6c97a12-c6b4-f178-acc1-567673523d06'),
('af0ef471-7dca-da91-6bc7-56616e9eb398', '2017-04-06 12:32:45', 1, 'ce7c7bb1-c511-82e5-40bf-56616a7a10cc', 'ae606f3d-5370-9abf-369a-56616e559357'),
('af4cbeee-9f5a-d55a-5fbc-58cf9d6f385e', '2017-03-20 10:57:33', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', 'aed7b370-1c60-2390-716c-58cf9d9f3d0c'),
('b530b867-498a-bd70-65cc-58cfb526254b', '2017-03-20 13:26:13', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', 'b4b53dca-3ca7-a5ad-84fd-58cfb507e8a0'),
('b7d42855-3f4a-4bec-ada7-58cf9def096e', '2017-03-20 10:57:33', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', 'b75a7e3a-3a04-e644-bbeb-58cf9de32173'),
('ba70dbac-b9c4-b01a-ceb7-56616ef2ea42', '2017-04-06 12:32:45', 1, 'ce7c7bb1-c511-82e5-40bf-56616a7a10cc', 'b9d7250f-4971-05b8-ebe2-56616e43f6bc'),
('c02eaa05-8b85-9af0-e9e8-58cfb53f574b', '2017-03-20 13:26:13', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', 'bf7d328c-47d4-8d1c-bafe-58cfb5cc2d81'),
('cb55beaf-b39f-8d78-c379-58cfb503c909', '2017-03-20 13:26:13', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', 'cadc200c-4de2-9c78-f75a-58cfb5f67856'),
('cd3a0876-9928-64ec-2d1f-566aab4e8926', '2015-12-11 10:56:48', 0, 'bb9311ed-8e1b-7715-9222-565f2e684b49', 'b15c0722-19da-79bf-51b2-566aabefc417'),
('d24a2cd1-0922-9e67-3c8a-56616fa70aef', '2017-04-06 12:32:45', 1, 'ce7c7bb1-c511-82e5-40bf-56616a7a10cc', 'd203b35d-38b2-3d9c-1b46-56616f474d3c'),
('d91d8d9d-6852-25e9-5f0b-56616f917b61', '2017-04-06 12:32:45', 1, 'ce7c7bb1-c511-82e5-40bf-56616a7a10cc', 'd8c9630e-4299-6767-e0c4-56616f979c7e'),
('db20a6c0-30b6-87d4-dd8f-577b8154c1b5', '2016-07-05 09:44:04', 0, 'c608ec6d-960e-eb4f-760d-577b81007df9', 'd9fed2e5-a303-e3d6-0c0c-577b814f9e4c'),
('de0951ca-9677-b28e-99a9-56616f275c90', '2017-04-06 12:32:45', 1, 'ce7c7bb1-c511-82e5-40bf-56616a7a10cc', 'ddc93703-d567-a464-22bc-56616f69224c'),
('e4c778d6-5edf-a815-afb4-58cf9bc573f8', '2017-03-20 09:10:57', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', 'e44fe420-a396-d951-cb7c-58cf9ba34f8a'),
('e61ea6cf-aafc-b1d8-f7d8-56616ff24565', '2017-04-06 12:32:45', 1, 'ce7c7bb1-c511-82e5-40bf-56616a7a10cc', 'e5ce5ab1-2e65-2235-400e-56616fb9f5f6'),
('e9c70b0c-f5d5-c08f-4249-58cf9a23ca90', '2017-03-20 09:03:40', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', 'e1ce0b22-74ab-11e8-b4c8-58cf9a86acf8'),
('ebe3092e-a344-00f4-8b5e-57728661f273', '2017-04-06 12:18:23', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', 'c3d209bb-0a2b-a6a9-f215-5772867b4603'),
('ee071730-fd54-a102-de1e-58cf9a9662fd', '2017-03-20 09:03:55', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', 'ed65a8a5-f409-b7f2-c171-58cf9a17cc85'),
('eee4ddd2-d929-838f-2527-58cf9bf35c6b', '2017-03-20 09:10:57', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', 'ee7c27b6-9739-e6d0-7749-58cf9b82a093'),
('f1470127-1225-5257-b1cc-58cf9bea895c', '2017-03-20 09:10:57', 1, '3d87bac4-49b0-3acb-3010-56684570afd9', 'e9f2030e-76f7-852e-3e7d-58cf9b407424');
-- --------------------------------------------------------
--
-- Struttura della tabella `tasks`
--
CREATE TABLE IF NOT EXISTS `tasks` (
`id` char(36) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`date_entered` datetime DEFAULT NULL,
`date_modified` datetime DEFAULT NULL,
`modified_user_id` char(36) DEFAULT NULL,
`created_by` char(36) DEFAULT NULL,
`description` text,
`deleted` tinyint(1) DEFAULT '0',
`assigned_user_id` char(36) DEFAULT NULL,
`status` varchar(100) DEFAULT 'Not Started',
`date_due_flag` tinyint(1) DEFAULT '0',
`date_due` datetime DEFAULT NULL,
`date_start_flag` tinyint(1) DEFAULT '0',
`date_start` datetime DEFAULT NULL,
`parent_type` varchar(255) DEFAULT NULL,
`parent_id` char(36) DEFAULT NULL,
`contact_id` char(36) DEFAULT NULL,
`priority` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_tsk_name` (`name`),
KEY `idx_task_con_del` (`contact_id`,`deleted`),
KEY `idx_task_par_del` (`parent_id`,`parent_type`,`deleted`),
KEY `idx_task_assigned` (`assigned_user_id`),
KEY `idx_task_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
--
-- Dump dei dati per la tabella `tasks`
--
INSERT INTO `tasks` (`id`, `name`, `date_entered`, `date_modified`, `modified_user_id`, `created_by`, `description`, `deleted`, `assigned_user_id`, `status`, `date_due_flag`, `date_due`, `date_start_flag`, `date_start`, `parent_type`, `parent_id`, `contact_id`, `priority`) VALUES
('15c65e64-83d5-e27e-9ded-56efc6034ec7', 'c1lesson1', '2016-03-21 10:02:12', '2017-04-06 12:18:23', '1', '1', NULL, 1, '1', 'Completed', 0, '2016-03-18 10:45:00', 0, '2016-03-18 10:00:00', NULL, '', '', 'High'),
('16ce0346-064f-c93e-19ca-5668427c30b4', 'c2lesson_first_completed', '2015-12-09 15:03:11', '2017-04-06 12:29:16', '1', '1', NULL, 0, '1', 'Completed', 0, '2015-12-10 11:00:00', 0, '2015-12-10 10:00:00', NULL, '', '', 'High'),
('18f60d90-0554-4880-760b-5668455dd961', 'c5lesson1', '2015-12-09 15:16:30', '2017-04-06 12:36:42', '1', '1', NULL, 0, '1', 'Not Started', 0, '2015-12-12 11:00:00', 0, '2015-12-12 10:00:00', NULL, '', '', 'High'),
('226c2475-16a3-e0af-efaa-5671a483f0e2', 'nocourse_lesson2', '2015-12-16 17:52:23', '2017-04-06 12:40:24', '1', '1', NULL, 0, '1', 'Not Started', 0, '2015-12-17 11:00:00', 0, '2015-12-17 10:00:00', NULL, '', '', 'High'),
('2996ca74-f2d5-a0bf-0c92-56616d7db8d4', 'c4lesson3', '2015-12-04 10:41:15', '2017-04-06 12:34:36', '1', 'c0aa6b17-16d0-baea-c4ae-566162ba3706', NULL, 0, 'c0aa6b17-16d0-baea-c4ae-566162ba3706', 'Completed', 0, '2015-10-05 09:30:00', 0, '2015-10-05 08:45:00', NULL, '', '', 'High'),
('2f19dd24-1c2f-a245-1856-566b214d7abd', 'c1lesson_last_completed', '2015-12-11 19:16:20', '2017-04-06 12:26:34', '1', '1', NULL, 0, '1', 'Completed', 0, '2015-12-16 11:00:00', 0, '2015-12-16 10:00:00', NULL, '', '', 'High'),
('39109517-1f24-8048-4fca-56616e12fc48', 'c4lesson4', '2015-12-04 10:45:59', '2017-04-06 12:35:11', '1', 'c0aa6b17-16d0-baea-c4ae-566162ba3706', NULL, 0, 'c0aa6b17-16d0-baea-c4ae-566162ba3706', 'Completed', 0, '2015-10-08 09:30:00', 0, '2015-10-08 08:45:00', NULL, '', '', 'High'),
('3b0e7a1e-543c-ea43-5a5c-565ee2d8a787', 'GWENT - VR', '2015-12-02 12:20:48', '2017-04-06 12:39:20', '1', 'e07880a3-a57d-a20c-9e25-565eb7af093f', NULL, 0, '36fcf0ba-7cd8-1195-2825-5655fd33eabd', 'Not Started', 0, '2015-10-14 11:30:00', 0, '2015-10-14 10:45:00', NULL, '', '', 'High'),
('3d62fc14-f606-a737-d76c-565ee38e767b', 'GWENT - VR', '2015-12-02 12:28:27', '2017-04-06 12:39:20', '1', 'e07880a3-a57d-a20c-9e25-565eb7af093f', NULL, 1, '36fcf0ba-7cd8-1195-2825-5655fd33eabd', 'Not Started', 0, '2015-10-23 11:30:00', 0, '2015-10-23 10:45:00', NULL, '', '', 'High'),
('4dd39006-f652-3ae1-d6a6-56718c46617e', 'maghull test 2', '2015-12-16 16:09:01', '2017-04-06 12:37:27', '1', '1', NULL, 1, '1', 'Not Started', 0, '2015-12-12 11:00:00', 0, '2015-12-12 10:00:00', NULL, '', '', 'High'),
('595a2e58-76bc-c134-3cef-58e64b59e8d5', 'c1lesson_last_completed', '2017-04-06 14:08:03', '2017-03-16 14:09:28', '1', '1', NULL, 0, '1', 'Completed', 0, '2015-12-16 11:00:00', 0, '2015-12-16 10:00:00', NULL, '', '', 'High'),
('5ba36af1-8f2b-075b-926f-58cfd87a83e7', 'c1lesson_notyetcompleted', '2017-03-20 13:26:40', '2017-04-06 12:27:12', '1', '1', NULL, 0, 'a691ed76-ef37-ad18-4450-561397563eee', 'Not Started', 1, NULL, 1, NULL, NULL, '', '', 'High'),
('6693cd37-0b74-1d97-c395-5672a343260d', 'nocourse_lesson1', '2015-12-17 11:59:11', '2017-04-06 12:39:59', '1', '1', NULL, 0, '1', 'Completed', 0, '2015-12-18 15:00:00', 0, '2015-12-18 14:00:00', NULL, '', '', 'High'),
('7488d168-758c-f760-ed28-57738b5565db', 'c1lesson1', '2016-06-29 08:47:35', '2017-04-06 12:15:28', '1', '1', NULL, 0, '1', 'Completed', 0, '2015-04-06 09:45:00', 0, '2015-04-06 09:00:00', NULL, '', '', 'High'),
('760f5ee8-6470-9291-6733-56718c949974', 'c5lesson2', '2015-12-16 16:09:30', '2017-04-06 12:37:50', '1', '1', NULL, 0, '1', 'Not Started', 0, '2015-12-14 11:00:00', 0, '2015-12-14 10:00:00', NULL, '', '', 'High'),
('80859612-d91f-bd91-8198-577b81c9dd09', 'c3lesson2', '2016-07-05 09:44:33', '2017-04-06 12:24:08', '1', '1', NULL, 0, '1', 'Completed', 1, NULL, 0, '2016-07-05 09:00:00', NULL, '', '', 'High'),
('8a635f3b-9e0f-16aa-5532-58e631257729', 'c1lesson_first_completed', '2017-04-06 12:16:49', '2017-04-06 12:27:36', '1', '1', NULL, 0, '1', 'Completed', 0, '2015-04-06 09:45:00', 0, '2015-04-06 09:00:00', NULL, '', '', 'High'),
('8c8404b6-4e53-5f2b-fcce-577b84f9dceb', 'c3lesson_last_completed', '2016-07-05 09:58:43', '2017-04-06 12:31:22', '1', '1', NULL, 0, '1', 'Completed', 1, NULL, 0, '2016-07-06 09:00:00', NULL, '', '', 'High'),
('92a4c747-b1b7-2949-bcf6-56616ee3b065', 'c4lesson2', '2015-12-04 10:43:35', '2017-04-06 12:33:52', '1', 'c0aa6b17-16d0-baea-c4ae-566162ba3706', NULL, 0, 'a691ed76-ef37-ad18-4450-561397563eee', 'Completed', 0, '2015-10-04 09:30:00', 0, '2015-10-04 08:45:00', NULL, '', '', 'High'),
('9ecdc18e-9187-da7e-54d9-58e635cddf77', 'c4lesson2', '2017-04-06 12:34:10', '2017-04-06 12:34:10', '1', '1', '', 0, 'a691ed76-ef37-ad18-4450-561397563eee', 'Not Started', 0, '2015-10-04 09:30:00', 0, '2015-10-04 08:45:00', NULL, NULL, NULL, 'High'),
('a2058842-e181-b794-6343-56616ef2e0b2', 'c4lesson4_last_completed', '2015-12-04 10:43:35', '2017-04-06 12:35:49', '1', 'c0aa6b17-16d0-baea-c4ae-566162ba3706', NULL, 0, 'a691ed76-ef37-ad18-4450-561397563eee', 'Not Started', 0, '2015-10-19 09:30:00', 0, '2015-10-19 08:45:00', NULL, '', '', 'High'),
('a6c97a12-c6b4-f178-acc1-567673523d06', 'c4lesson_first_completed', '2015-12-20 09:25:15', '2017-04-06 12:33:24', '1', '1', NULL, 0, '1', 'Completed', 0, '2015-07-02 09:00:00', 0, '2015-07-02 09:00:00', NULL, '', '', 'High'),
('b15c0722-19da-79bf-51b2-566aabefc417', 'c2lesson_last_completed', '2015-12-11 10:56:48', '2017-04-06 12:29:58', '1', '1', NULL, 0, '1', 'Completed', 0, '2015-12-16 11:00:00', 0, '2015-12-16 10:00:00', NULL, '', '', 'High'),
('d9fed2e5-a303-e3d6-0c0c-577b814f9e4c', 'c3lesson_first_completed', '2016-07-05 09:44:04', '2017-04-06 12:30:54', '1', '1', NULL, 0, '1', 'Completed', 1, NULL, 0, '2016-07-01 09:00:00', NULL, '', '', 'High'),
('fb94089f-57e9-0918-c98f-56756fa424d4', 'c1lesson2', '2015-12-19 14:55:56', '2017-04-06 12:17:29', '1', '1', NULL, 0, '1', 'Completed', 0, '2015-10-06 09:00:00', 0, '2015-10-06 09:00:00', NULL, '', '', 'High');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

通过以下查询,我可以获得所有“已完成”课程的列表,但是我很难在每个课程中获得最新的课程...实际上我需要第一行,因为每门课程:
我不是SQL专家,我非常感谢任何帮助。谢谢。
Piergiorgio
SELECT pj001_courses.name AS Corso,
pj001_courses.id AS id_corso,
tasks.id AS idlez,
tasks.name AS Lezione,
tasks.date_modified AS date_modified,
max_dates.max_date_start AS date_start
FROM pj001_courses
JOIN pj001_courses_tasks_c ON pj001_courses.id = pj001_courses_tasks_c.pj001_courses_taskspj001_courses_ida
JOIN tasks ON pj001_courses_tasks_c.pj001_courses_taskstasks_idb = tasks.id
JOIN ( SELECT pj001_courses_tasks_c.pj001_courses_taskspj001_courses_ida AS id_corso,
MAX( tasks.date_start ) AS max_date_start
FROM pj001_courses_tasks_c
JOIN tasks ON pj001_courses_tasks_c.pj001_courses_taskstasks_idb = tasks.id
WHERE pj001_courses_tasks_c.deleted = 0
AND tasks.deleted = 0
AND tasks.status = 'completed'
GROUP BY id_corso ) max_dates ON max_dates.id_corso = pj001_courses.id
AND max_dates.max_date_start = tasks.date_start
WHERE pj001_courses.deleted = 0
ORDER BY Corso,
id_corso,
tasks.date_start DESC,
tasks.date_modified desc;

答案 0 :(得分:0)
请尝试......
SELECT pj001_courses.name AS Corso,
pj001_courses.id AS id_corso,
tasks.id AS idlez,
tasks.name AS Lezione,
tasks.date_modified AS date_modified,
tasks.date_start AS date_start
FROM pj001_courses
JOIN pj001_courses_tasks_c ON pj001_courses.id = pj001_courses_tasks_c.pj001_courses_taskspj001_courses_ida
JOIN tasks ON pj001_courses_tasks_c.pj001_courses_taskstasks_idb = tasks.id
JOIN
(
SELECT pj001_courses_tasks_c.pj001_courses_taskspj001_courses_ida AS id_corso,
tasks.date_start AS max_date_start,
MAX( tasks.date_modified ) AS max_date_modified
FROM pj001_courses_tasks_c
JOIN tasks ON pj001_courses_tasks_c.pj001_courses_taskstasks_idb = tasks.id
WHERE ( pj001_courses_tasks_c.pj001_courses_taskspj001_courses_ida,
tasks.date_start ) IN ( SELECT pj001_courses_tasks_c.pj001_courses_taskspj001_courses_ida AS id_corso,
MAX( tasks.date_start ) AS max_date_start
FROM pj001_courses_tasks_c
JOIN tasks ON pj001_courses_tasks_c.pj001_courses_taskstasks_idb = tasks.id
WHERE pj001_courses_tasks_c.deleted = 0
AND tasks.deleted = 0
AND tasks.status = 'completed'
GROUP BY id_corso
)
GROUP BY id_corso
) max_dates_finder ON pj001_courses.id = max_dates_finder.id_corso
AND tasks.date_start = max_dates_finder.max_date_start
AND tasks.date_modified = max_dates_finder.max_date_modified
WHERE pj001_courses.deleted = 0
ORDER BY Corso,
id_corso;
以下分段生成分配给每个课程的单元列表,其中该单元已完成且尚未从课程中删除或一般性地删除。然后,它会为每门课程输出每门课程的id
和最新的date_start
值。
SELECT pj001_courses_tasks_c.pj001_courses_taskspj001_courses_ida AS id_corso,
MAX( tasks.date_start ) AS max_date_start
FROM pj001_courses_tasks_c
JOIN tasks ON pj001_courses_tasks_c.pj001_courses_taskstasks_idb = tasks.id
WHERE pj001_courses_tasks_c.deleted = 0
AND tasks.deleted = 0
AND tasks.status = 'completed'
GROUP BY id_corso
以下细分产生一个课程列表和分配给每个课程的单元,其中每个条目的课程id
和date_start
出现在前一个细分生成的列表中。然后,此新列表会优化为课程id
,该课程的date_start
的最新值以及与该date_modified
的值对应的date_start
的最新值。
SELECT pj001_courses_tasks_c.pj001_courses_taskspj001_courses_ida AS id_corso,
tasks.date_start AS max_date_start,
MAX( tasks.date_modified ) AS max_date_modified
FROM pj001_courses_tasks_c
JOIN tasks ON pj001_courses_tasks_c.pj001_courses_taskstasks_idb = tasks.id
WHERE ( pj001_courses_tasks_c.pj001_courses_taskspj001_courses_ida,
tasks.date_start ) IN ( SELECT pj001_courses_tasks_c.pj001_courses_taskspj001_courses_ida AS id_corso,
MAX( tasks.date_start ) AS max_date_start
FROM pj001_courses_tasks_c
JOIN tasks ON pj001_courses_tasks_c.pj001_courses_taskstasks_idb = tasks.id
WHERE pj001_courses_tasks_c.deleted = 0
AND tasks.deleted = 0
AND tasks.status = 'completed'
GROUP BY id_corso
)
GROUP BY id_corso
然后将此代码JOIN
与其他表一起编辑,并仅对那些尚未删除的课程进行优化,以便从我的答案开头生成代码。
如果您有任何问题或意见,请随时发表评论。
答案 1 :(得分:0)
@toonice上次查询中的某些字段名称不正确。我修复了它们,但无论如何结果看起来并不一致。 date_start和date_modified。我是Stack Overflow中的新手,并且不确定我是否可以附加只有30/40条记录的测试数据库用于查询测试purpouse
SELECT pj001_courses.name AS Corso,
pj001_courses.id AS id_corso,
tasks.id AS idlez,
tasks.name AS Lezione,
tasks.date_start AS date_start,
MAX( tasks.date_modified ) AS date_modified
FROM pj001_courses
JOIN pj001_courses_tasks_c ON pj001_courses.id = pj001_courses_tasks_c.pj001_courses_taskspj001_courses_ida
JOIN tasks ON pj001_courses_tasks_c.pj001_courses_taskstasks_idb = tasks.id
WHERE tasks.deleted = 0
AND tasks.status = 'completed'
AND pj001_courses.deleted = 0
GROUP BY id_corso
ORDER BY Corso,
id_corso,
tasks.date_start DESC,
tasks.id;