我有两张table
供应商
ID | userid| address | Country
1 | 10 | NY | US
2 | 20 | Mumbai | INDIA
事件_待办事项
ID | events_id| vendor| status
1 | 1 | 10 | Completed
2 | 2 | 20 | Inprogress
因此,我希望联接这两个表并获取所有供应商表数据。我希望根据vendor
表的userid
是否存在于event_todo
表的vendor
中来进行联接。我还希望在状态为已完成时显示Count的列。例如:
ID | userid | address | Country | events_id | status | Count
1 | 10 | NY | US | 1 | Completed | 1
2 | 20 | Mumbai | INDIA | 2 | Inprogress| 0
我已经应用了以下查询,并获得了结果,但无法获得事件状态的计数
SELECT `vendors`.`id`, `vendors`.`userid`, `vendors`.`address`,`vendors`.`country` AS `updatedAt`, `vendors`.`userid` IN (SELECT sum(events_todo.status) AS completed FROM events_todo ) AS `completed`, `events_todo`.`id` AS `events_todo.id`, `events_todo`.`events_id` AS `events_todo.events_id`, `events_todo`.`category` `events_todo.vendor`, `events_todo`.`created_by` `events_todo.status` FROM `vendors` AS `vendors` LEFT OUTER JOIN `events_todo` AS `events_todo` ON `vendors`.`userid` = `events_todo`.`vendor` WHERE (`vendors`.`city` LIKE '%%' AND `vendors`.`state` LIKE '%%' AND `vendors`.`country` LIKE '%%') AND events_todo.status IS NOT NULL
我得到的状态计数为0,似乎总和工作不正常。请建议需要做什么。
1条答案
按热度按时间5rgfhyps1#
您可以使用如下简单查询
输出
| 标识符|用户标识|位址|乡村|事件标识|状态|计数器|
| - -|- -|- -|- -|- -|- -|- -|
| 一个|10个|纽约州|美国|一个|已完成|一个|
| 2个|20个|孟买|印度Name| 2个|进行中|第0页|