- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个概念是这样的:我们有卖家和买家。卖家创建任务,买家发起聊天并开始对话。因此,卖家可以针对一项任务与不同的买家进行多次聊天(每个买家一次聊天,买家只能发起一次聊天,但他可以发送多条消息)并且一次聊天将包含多条消息。该消息具有作为卖家或买家的 author_id。
所以我想找到两件事(两个查询):一是我想找到所有没有 author_id 等于 tasks.seller_id 消息的任务(或者根本没有消息)。
其次,我想找到相反的任务,其中至少有一条消息的 author_id 等于 seller_id。第二个是以下查询(感谢@bill-karwin):
SELECT t.id, COUNT(*) AS sellerMessages
FROM tasks AS t
INNER JOIN chats AS c ON c.task_id = t.id
INNER JOIN messages AS m ON m.chat_id = c.id
AND t.seller_id = m.author_id
GROUP BY t.id
HAVING sellerMessages > 0
ORDER BY t.id;
如何找到相反的位置?
更新
我已经粘贴了一个带有记录的 sql 脚本来测试:http://pastebin.com/KmMJjQsR它创建 60 条不同的任务记录及其消息和聊天关联。
上面的 sql 查询返回 31 个任务,它是正确的。我需要找到上面的反向,它将返回其他 29 个任务。
创建脚本
DROP TABLE IF EXISTS `tasks`;
CREATE TABLE `tasks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`seller_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `index_tasks_on_seller_id` (`seller_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `tasks` (`id`, `seller_id`)
VALUES
(15788, 95157),
(15789, 95158),
(15790, 86270),
(15791, 86270),
(15792, 94260),
(15793, 14354),
(15794, 95160),
(15795, 95161),
(15796, 10786),
(15797, 95162),
(15798, 72740),
(15799, 95163),
(15800, 93159),
(15801, 95165),
(15802, 95168),
(15803, 95170),
(15804, 95173),
(15805, 6983),
(15806, 95175),
(15807, 95177),
(15808, 80037),
(15809, 45066),
(15810, 95180),
(15811, 95181),
(15812, 95182),
(15813, 84020),
(15814, 95156),
(15815, 93418),
(15816, 74020),
(15817, 74020),
(15818, 73604),
(15819, 95190),
(15820, 95188),
(15821, 39132),
(15822, 95191),
(15823, 95192),
(15824, 95196),
(15825, 32979),
(15826, 30104),
(15827, 95198),
(15828, 95200),
(15829, 93974),
(15830, 95201),
(15831, 95202),
(15832, 33487),
(15833, 60076),
(15834, 33487),
(15835, 88081),
(15836, 95204),
(15837, 95205),
(15838, 27527),
(15839, 93055),
(15840, 95209),
(15841, 2297),
(15842, 95211),
(15843, 76806),
(15844, 69400),
(15845, 34273),
(15846, 95214),
(15847, 82877);
DROP TABLE IF EXISTS `chats`;
CREATE TABLE `chats` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`creator_id` int(11) DEFAULT NULL,
`task_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `chats` (`id`, `creator_id`, `task_id`)
VALUES
(10754, 53058, 15788),
(10767, 36414, 15788),
(10778, 61359, 15788),
(10759, 61359, 15789),
(10770, 36414, 15789),
(10961, 10541, 15790),
(10777, 61359, 15792),
(10819, 11835, 15793),
(10766, 4631, 15795),
(10791, 18461, 15795),
(10823, 31111, 15795),
(10874, 11649, 15795),
(10994, 85902, 15795),
(10769, 36414, 15797),
(10776, 61359, 15797),
(10919, 53058, 15797),
(10771, 12815, 15798),
(10775, 83339, 15798),
(10811, 12745, 15799),
(11211, 35416, 15799),
(10793, 4631, 15800),
(10833, 85902, 15800),
(10816, 36414, 15802),
(10806, 36414, 15803),
(10801, 80049, 15804),
(10818, 36414, 15804),
(10824, 11835, 15805),
(10802, 7240, 15806),
(10822, 39871, 15806),
(10838, 79168, 15806),
(10817, 36414, 15807),
(10918, 53058, 15807),
(10809, 33051, 15808),
(10843, 20435, 15808),
(10803, 45069, 15809),
(10804, 5093, 15809),
(10814, 84699, 15810),
(10844, 58098, 15810),
(10847, 95206, 15810),
(10849, 95105, 15810),
(10853, 94009, 15810),
(10854, 94009, 15810),
(10855, 94009, 15810),
(10856, 94009, 15810),
(10857, 94009, 15810),
(10858, 94009, 15810),
(10859, 94009, 15810),
(10860, 94009, 15810),
(10894, 65435, 15814),
(11057, 91171, 15815),
(10975, 85902, 15816),
(10812, 4631, 15817),
(10841, 85115, 15818),
(10903, 87971, 15818),
(10980, 36414, 15819),
(10886, 39393, 15821),
(10825, 11835, 15824),
(10871, 2919, 15824),
(11194, 21322, 15824),
(11137, 94553, 15825),
(10831, 84932, 15826),
(10836, 83339, 15826),
(10839, 8532, 15826),
(10840, 11967, 15826),
(10913, 35573, 15827),
(10983, 36414, 15827),
(10878, 29425, 15829),
(10952, 36414, 15831),
(10845, 10511, 15832),
(10941, 7240, 15832),
(10872, 3154, 15834),
(10897, 36414, 15835),
(10917, 53058, 15835),
(11045, 36902, 15835),
(10879, 29425, 15836),
(11114, 94682, 15836),
(10900, 36414, 15837),
(10846, 4631, 15838),
(10850, 4631, 15839),
(10848, 4631, 15841),
(10852, 4631, 15842),
(10851, 4631, 15844),
(10864, 62822, 15845),
(10889, 85115, 15847);
DROP TABLE IF EXISTS `messages`;
CREATE TABLE `messages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`chat_id` int(11) DEFAULT NULL,
`author_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `messages` (`chat_id`, `author_id`)
values
(10754, 53058),
(10767, 36414),
(10778, 61359),
(10759, 61359),
(10759, 95158),
(10770, 36414),
(10770, 95158),
(10961, 10541),
(10961, 86270),
(10777, 61359),
(10819, 11835),
(10819, 14354),
(10766, 4631),
(10766, 95161),
(10791, 95161),
(10791, 18461),
(10823, 31111),
(10874, 11649),
(10994, 85902),
(10769, 36414),
(10776, 61359),
(10919, 53058),
(10775, 83339),
(10775, 72740),
(10811, 12745),
(10811, 95163),
(11211, 35416),
(11211, 95163),
(10793, 4631),
(10793, 93159),
(10833, 85902),
(10833, 93159),
(10816, 36414),
(10806, 36414),
(10801, 80049),
(10818, 36414),
(10824, 11835),
(10824, 6983),
(10802, 7240),
(10802, 95175),
(10822, 39871),
(10838, 79168),
(10838, 95175),
(10817, 36414),
(10817, 95177),
(10918, 53058),
(10809, 33051),
(10809, 80037),
(10843, 20435),
(10843, 80037),
(10803, 45069),
(10804, 5093),
(10814, 84699),
(10814, 95180),
(10844, 58098),
(10844, 95180),
(10847, 95206),
(10847, 95180),
(10849, 95105),
(10849, 95180),
(10853, 94009),
(10853, 95180),
(10894, 65435),
(10894, 95156),
(11057, 91171),
(10975, 85902),
(10975, 74020),
(10812, 4631),
(10812, 74020),
(10841, 85115),
(10841, 73604),
(10903, 87971),
(10903, 73604),
(10980, 36414),
(10980, 95190),
(10886, 39393),
(10886, 39132),
(10825, 11835),
(10825, 95196),
(10871, 2919),
(10871, 95196),
(11194, 21322),
(11194, 95196),
(11137, 94553),
(10831, 84932),
(10836, 83339),
(10839, 8532),
(10839, 30104),
(10840, 11967),
(10913, 35573),
(10913, 95198),
(10983, 36414),
(10878, 29425),
(10878, 93974),
(10952, 36414),
(10845, 33487),
(10845, 10511),
(10941, 7240),
(10872, 3154),
(10872, 33487),
(10897, 36414),
(10897, 88081),
(10917, 53058),
(11045, 36902),
(11045, 88081),
(10879, 29425),
(10879, 95204),
(11114, 94682),
(10900, 36414),
(10900, 95205),
(10846, 4631),
(10846, 27527),
(10850, 4631),
(10850, 93055),
(10848, 4631),
(10852, 4631),
(10852, 95211),
(10851, 4631),
(10851, 69400),
(10864, 62822),
(10889, 85115);
结果:
在一个查询中(消息中包含 sender_id
的查询)应该返回 31
或工作 ID
15789,15790,15793,15795,15798,15799,15800,
15805,15806,15807,15808,15810,15814,15816,
15817,15818,15819,15821,15824,15826,15827,
15829,15832,15834,15835,15836,15837,15838,
15839,15842,15844
在另一个查询中,结果应该是
15841,15796,15825,15845,15809,15833,15843,
15847,15813,15791,15815,15792,15788,15794,
15797,15801,15802,15803,15804,15811,15812,
15820,15822,15823,15828,15830,15831,15840,15846
或29
。
最佳答案
I want to find ... tasks with at least one message with
author_id
equals toseller_id
.
您在问题中的查询可以稍微简化一下。不需要显式的 HAVING
过滤器,因为 INNER JOINs
可以做到。如果不需要消息计数,可以省略 COUNT
。
SELECT t.id
FROM tasks AS t
INNER JOIN chats AS c ON c.task_id = t.id
INNER JOIN messages AS m
ON m.chat_id = c.id
AND t.seller_id = m.author_id
GROUP BY t.id
ORDER BY t.id;
I want to find all the tasks which don't have messages with
author_id
equal totasks.seller_id
(or don't have messages at all).
获取顶级查询的逆向的一种非常直接的方法是将其用作 NOT IN
的子查询:
SELECT tasks.id
FROM tasks
WHERE
tasks.id NOT IN
(
SELECT t.id
FROM tasks AS t
INNER JOIN chats AS c ON c.task_id = t.id
INNER JOIN messages AS m
ON m.chat_id = c.id
AND t.seller_id = m.author_id
)
;
关于Mysql group_by语句的逆向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39375254/
创建一个“海盗对话”,可以选择左手或右手。我希望它对“左”和“右”的不同拼写做出积极的回答(正如您将在代码中看到的那样),但是,当我为所有非“右”或“左”的输入添加最终的“else”代码时,它给了我一
With 语句 对一个对象执行一系列的语句。 With object statements End With 参数 object 必需的部分
While...Wend 语句 当指定的条件为 True 时,执行一系列的语句。 While condition  ; Version [stat
所以我正在处理的代码有一个小问题。 while True: r = input("Line: ") n = r.split() if r == " ":
我有一个对象数组: var contacts = [ { "firstName": "Akira", "lastName": "Laine", "number"
int main() { int f=fun(); ... } int fun() { return 1; return 2; } 在上面的程序中,当从main函数中调用一个
我的项目中有很多 if 语句、嵌套 if 语句和 if-else 语句,我正在考虑将它们更改为 switch 语句。其中一些将具有嵌套的 switch 语句。我知道就编译而言,switch 语句通常更
Rem 语句 包含程序中的解释性注释。 Rem comment 或 ' comment comment 参数是需要包含的注释文本。在 Rem 关键字和 comment 之间应有一个空格。
ReDim 语句 在过程级中声明动态数组变量并分配或重新分配存储空间。 ReDim [Preserve] varname(subscripts) [, varname(subscripts)]
Randomize 语句 初始化随机数生成器。 Randomize [number] number 参数可以是任何有效的数值表达式。 说明 Randomize 使用 number 参数初始
Public 语句 定义公有变量并分配存储空间。在 Class 块中定义私有变量。 Public varname[([subscripts])][, varname[([subscripts])
Sub 语句 声明 Sub 过程的名称、参数以及构成其主体的代码。 [Public [Default]| Private] Sub name [( arglist )]
Set 语句 将对象引用赋给一个variable或property,或者将对象引用与事件关联。 Set objectvar = {objectexpression | New classname
我有这个代码块,有时第一个 if 语句先运行,有时第二个 if 语句先运行。我不确定为什么会这样,因为我认为 javascript 是同步的。 for (let i = 0; i < dataObje
这是一个 javascript 代码,我想把它写成这样:如果此人回答是,则回复“那很酷”,如果此人回答否,则回复“我会让你开心”,如果此人回答的问题包含"is"或“否”,请说“仅键入”是或否,没有任何
这是我的任务,我尝试仅使用简短的 if 语句来完成此任务,我得到的唯一错误是使用“(0.5<=ratio<2 )”,除此之外,构造正确吗? Scanner scn = new Scanner(
有没有办法在 select 语句中使用 if 语句? 我不能在这个中使用 Case 语句。实际上我正在使用 iReport 并且我有一个参数。我想要做的是,如果用户没有输入某个参数,它将选择所有实例。
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: If vs. Switch Speed 我将以 C++ 为例,但我要问的问题不是针对特定语言的。我的意思是一
Property Set 语句 在 Class 块中,声明名称、参数和代码,这些构成了将引用设置到对象的 Property 过程的主体。 [Public | Private] Pro
Property Let 语句 在 Class 块中,声明名称、参数和代码等,它们构成了赋值(设置)的 Property 过程的主体。 [Public | Private] Prop
我是一名优秀的程序员,十分优秀!