gpt4 book ai didi

MySQL 选择公司未提供的任务

转载 作者:行者123 更新时间:2023-11-29 04:50:05 25 4
gpt4 key购买 nike

我有 2 个表,任务和报价,我想在公司(企业)没有报价的地方选择所有任务。我为两个表和一些示例数据都包含了 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

最佳答案

你唯一错过的就是这个

SELECT..
FROM..
WHERE..
AND `offer`.`taskid` IS NULL
ORDER BY `task`.`id` DESC

关于MySQL 选择公司未提供的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12872119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com