1

タスクとオファーの 2 つのテーブルがあり、会社 (ビジネス) がオファーを行っていないすべてのタスクを 1 回選択したいと考えています。両方のテーブルといくつかのサンプル データの SQL を含めました。私のクエリは以下のように見えますが、機能していません。

クエリ:

SELECT
  `task`.`id`,
  `task`.`carid`,
  `task`.`duedate`,
  `task`.`categoryid`,
  `task`.`offers`
FROM
  `task`
LEFT JOIN
  `offer`
ON
  `task`.`id` = `offer`.`taskid`
WHERE
  `task`.`offers` < 3
AND
  `offer`.`businessid` != 16
ORDER BY
  `task`.`id` DESC

テーブル:

CREATE TABLE `task` (
   `id` int(10) NOT NULL AUTO_INCREMENT,
   `carid` int(10) NOT NULL,
   `duedate` date NOT NULL,
   `categoryid` int(10) NOT NULL,
   `offers` int(1) NOT NULL DEFAULT '0',
   PRIMARY KEY (`id`),
   INDEX `carid` USING BTREE (carid),
   INDEX `categoryid` USING BTREE (categoryid)
) ENGINE=`InnoDB` AUTO_INCREMENT=3 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ROW_FORMAT=COMPACT CHECKSUM=0 DELAY_KEY_WRITE=0;

CREATE TABLE `offer` (
   `id` int(10) NOT NULL AUTO_INCREMENT,
   `businessid` int(10) NOT NULL,
   `taskid` int(10) NOT NULL,
   `price` decimal(10,2) NOT NULL,
   `status` enum('received','rejected','accepted','completed') NOT NULL DEFAULT 'received',
   PRIMARY KEY (`id`),
   INDEX `businessid` USING BTREE (businessid),
   INDEX `taskid` USING BTREE (taskid)
) ENGINE=`InnoDB` AUTO_INCREMENT=2 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ROW_FORMAT=COMPACT CHECKSUM=0 DELAY_KEY_WRITE=0;

サンプルデータ:

insert into `task` (1, 1, "2012-09-30", 3, 0);
insert into `task` (2, 1, "2012-09-27", 5, 0);
insert into `offer`(1, 16, 1, 3000, "received");
insert into `offer`(2, 13, 2, 212, "received");
insert into `offer`(3, 16, 2, 23, "received");

私はおそらくそれを解決したと思います:

SELECT
  `task`.`id`,
  `task`.`carid`,
  `task`.`categoryid`,
  `task`.`duedate`,
  `task`.`offers`
FROM
  task
LEFT JOIN
  `offer`
ON
  `task`.`id` = `offer`.`taskid`
WHERE
  task.id NOT IN (
    SELECT
      `offer`.`taskid`
    FROM
      `offer`
    WHERE
      `offer`.`businessid` = 16
  )
OR
  businessid IS NULL
AND
  offers < 3
GROUP BY
  task.id
ORDER BY
  duedate ASC
4

1 に答える 1

4

あなたが見逃したのはこれだけです

SELECT..
FROM..
WHERE..
         AND `offer`.`taskid` IS NULL
ORDER BY  `task`.`id` DESC
于 2012-10-13T10:15:27.937 に答える