phpmyadmin 获取第一个表的状态为已完成时的计数

5sxhfpxr  于 2022-11-09  发布在  PHP
关注(0)|答案(1)|浏览(84)

我有两张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,似乎总和工作不正常。请建议需要做什么。

5rgfhyps

5rgfhyps1#

您可以使用如下简单查询

SELECT v.id, v.user_id, v.address, v.country, e.events_id, e.status, IF(e.status = 'Completed', 1, 0) AS count
FROM vendors AS v
LEFT JOIN events_todo AS e ON e.vendor = v.user_id

输出

| 标识符|用户标识|位址|乡村|事件标识|状态|计数器|
| - -|- -|- -|- -|- -|- -|- -|
| 一个|10个|纽约州|美国|一个|已完成|一个|
| 2个|20个|孟买|印度Name| 2个|进行中|第0页|

相关问题