如何计算内部联接表中的行

时间:2018-11-18 11:01:42

标签: mysql sql count inner-join

我正在使用交叉引用的表提取所有信息以获取解决方案。

SELECT
    s.*, u.forname, u.surname, u.email, u.tel, p.type
FROM _user_solution s
INNER JOIN _users u
    ON s.uid = u.uid
INNER JOIN _payment_plans p
    ON p.pid = s.payment_plan_type

哪个工作正常,我的结果是否如预期。但是,我还有另一个表,该表包含该解决方案的任务,每个任务都有进度。我想尝试一下该解决方案有多少个任务:

SELECT
    s.*, u.forname, u.surname, u.email, u.tel, p.type,
    (SELECT COUNT(*) FROM t WHERE t.progress < 100 AS task)
FROM _user_solution s
INNER JOIN _users u
    ON s.uid = u.uid
INNER JOIN _payment_plans p
    ON p.pid = s.payment_plan_type
INNER JOIN _solution_tasks t
    ON s.sid = t.assigned_for_solution

但是我收到此错误:

  

您的SQL语法有错误;在第3行中,检查与您的MariaDB服务器版本相对应的手册,以在“ AS任务”附近使用正确的语法。FROM _user_solution s INNER JOIN _users u ON s.uid = u.uid

任何关于我如何计算该解决方案中不完整的任务的想法将不胜感激。

4 个答案:

答案 0 :(得分:1)

您需要将$color-list: ( "c1" : #ed0ead, "c2" : #12bc21, "c3" : #2666cc ); @mixin color-variants { @each $class, $color in $color-list { $myColor: $color !global; &.#{$class} { @content; } } } .my-module { padding: 1rem; background-color: #fff; border-top-size: 5px; border-top-style: solid; @include color-variants { border-top-color: $myColor; } } .another-module { padding: 1rem; border-radius: 5px; border-size: 2px; border-style: solid; @include color-variants { background-color: rgba($myColor, 0.5); &:hover { color: $myColor; } } } 别名部分移到子查询之外;在右括号之外。

AS task

答案 1 :(得分:1)

您可以尝试以下操作

SELECT
    s.*, u.forname, u.surname, u.email, u.tel, p.type,
    (SELECT COUNT(*) 
          FROM another_table tt1 
          WHERE tt1.taskID=t.taskID --assume taskID is join key 
         and tt1.progress < 100 
    ) AS task
FROM _user_solution s
INNER JOIN _users u
    ON s.uid = u.uid
INNER JOIN _payment_plans p
    ON p.pid = s.payment_plan_type
INNER JOIN _solution_tasks t
    ON s.sid = t.assigned_for_solution

答案 2 :(得分:1)

我设法弄清楚该怎么做,我需要将进度放在WHERE子句的底部,因为这是我要查询所有内容的原因,然后我需要选择COUNT(*) < / p>

SELECT
    s.*, u.forname, u.surname, u.email, u.tel, p.type, COUNT(*) as tasks
FROM _user_solution s
INNER JOIN _users u
    ON s.uid = u.uid
INNER JOIN _payment_plans p
    ON p.pid = s.payment_plan_type
INNER JOIN _solution_tasks t
    ON s.sid = t.assigned_for_solution
WHERE t.progress < 100

现在,这给了我多少解决方案分配给该解决方案。经过大量研究后,第一个COUNT为0,就像数组索引从0开始,因此,在这种情况下,解决方案行本身得出0,然后与解决方案关联的每个任务加1,为我提供了正确的倍数。任务。

答案 3 :(得分:0)

我已将您的查询更改为此,并且可以正常工作:


SELECT
    s.*, u.forname, u.surname, u.email, u.tel, p.type,
    (SELECT COUNT(WRITE_AUTOINCREMENT_ID) AS task FROM t WHERE t.progress < 100)
FROM _user_solution AS s
INNER JOIN _users AS u
    ON s.uid = u.uid
INNER JOIN _payment_plans p
    ON p.pid = s.payment_plan_type
INNER JOIN _solution_tasks AS ttable
    ON s.sid = ttable.assigned_for_solution

相关问题