sql转换成视图时参数错误
sql parameter error when convert into view
我是 sql 的新手,我想知道为什么当我在视图中创建 sql 查询时这个错误会出现
mysql 查询:
SELECT staffid AS staff,
(SELECT count( feedback )FROM msqueue WHERE staffid = staff AND feedback =1) AS a,
(SELECT count( feedback )FROM msqueue WHERE staffid = staff AND feedback =2) AS b,
(SELECT count( feedback )FROM msqueue WHERE staffid = staff AND feedback =3) AS c,
(SELECT count( feedback )FROM msqueue WHERE staffid = staff AND feedback =4) AS d
FROM msqueue
GROUP BY staffid
这个查询结果就是我想要的
|staff| a | b | c | d |
|d1111| 0 | 1 | 1 | 0 |
|d1234| 1 | 2 | 4 | 0 |
但是当我将查询创建为视图时
结果是
|staff| a | b | c | d |
|d1111| 1 | 3 | 5 | 0 |
|d1234| 1 | 3 | 5 | 0 |
请帮我看看这段代码是怎么回事..非常感谢
尝试
SELECT staffid AS staff,
(SELECT count(*) FROM msqueue n WHERE n.staffid = m.staffid AND feedback =1) AS a,
(SELECT count(*) FROM msqueue n WHERE n.staffid = m.staffid AND feedback =2) AS b,
(SELECT count(*) FROM msqueue n WHERE n.staffid = m.staffid AND feedback =3) AS c,
(SELECT count(*) FROM msqueue n WHERE n.staffid = m.staffid AND feedback =4) AS d
FROM msqueue m
GROUP BY staffid
在您的原始查询中,您使用主 select 的列 staffid
的别名将其与子查询的 staffid
列配对。这不足以区分两列,因此您得到了每一行中所有内容的总和。通过在列名前面使用 table 别名 m
和 n
我们现在可以清楚地解决这两个问题
列。
看这里:http://sqlfiddle.com/#!9/ab25a9/1
您甚至可以在子查询中省略 tables 别名,例如
SELECT staffid AS staff,
(SELECT count(*) FROM msqueue WHERE staffid = m.staffid AND feedback =1) AS a,
(SELECT count(*) FROM msqueue WHERE staffid = m.staffid AND feedback =2) AS b,
(SELECT count(*) FROM msqueue WHERE staffid = m.staffid AND feedback =3) AS c,
(SELECT count(*) FROM msqueue WHERE staffid = m.staffid AND feedback =4) AS d
FROM msqueue m
GROUP BY staffid
http://sqlfiddle.com/#!9/ab25a9/2
编辑
在我这边 "clever-mouthing" 之后,我刚刚发现,您的原始陈述有效 - 至少 "on its own" (http://sqlfiddle.com/#!9/ab25a9/4)。我还没有尝试将其放在视图中。也许这就是事情开始出错的时候。更安全的选择肯定是使用 table 别名。
是的,当你把它放在一个视图中时,确实会出错!但是只要外部查询至少有一个 table 别名,一切就OK了:http://sqlfiddle.com/#!9/f5f1e/1
我首先使用 MySql 对其进行了测试,但是语句是相同的,并且在 SQL-服务器,看这里http://sqlfiddle.com/#!6/f5f1e/1
我无法访问 mysql 服务器来对此进行测试,但总的来说 SQL 这可能是一个更简单的解决方案:
SELECT
staffid AS staff,
SUM(CASE WHEN feedback = 1 THEN 1 ELSE 0 END) AS a,
SUM(CASE WHEN feedback = 2 THEN 1 ELSE 0 END) AS b,
SUM(CASE WHEN feedback = 3 THEN 1 ELSE 0 END) AS c,
SUM(CASE WHEN feedback = 4 THEN 1 ELSE 0 END) AS d
FROM
msqueue
GROUP BY
staffid
我是 sql 的新手,我想知道为什么当我在视图中创建 sql 查询时这个错误会出现
mysql 查询:
SELECT staffid AS staff,
(SELECT count( feedback )FROM msqueue WHERE staffid = staff AND feedback =1) AS a,
(SELECT count( feedback )FROM msqueue WHERE staffid = staff AND feedback =2) AS b,
(SELECT count( feedback )FROM msqueue WHERE staffid = staff AND feedback =3) AS c,
(SELECT count( feedback )FROM msqueue WHERE staffid = staff AND feedback =4) AS d
FROM msqueue
GROUP BY staffid
这个查询结果就是我想要的
|staff| a | b | c | d |
|d1111| 0 | 1 | 1 | 0 |
|d1234| 1 | 2 | 4 | 0 |
但是当我将查询创建为视图时 结果是
|staff| a | b | c | d |
|d1111| 1 | 3 | 5 | 0 |
|d1234| 1 | 3 | 5 | 0 |
请帮我看看这段代码是怎么回事..非常感谢
尝试
SELECT staffid AS staff,
(SELECT count(*) FROM msqueue n WHERE n.staffid = m.staffid AND feedback =1) AS a,
(SELECT count(*) FROM msqueue n WHERE n.staffid = m.staffid AND feedback =2) AS b,
(SELECT count(*) FROM msqueue n WHERE n.staffid = m.staffid AND feedback =3) AS c,
(SELECT count(*) FROM msqueue n WHERE n.staffid = m.staffid AND feedback =4) AS d
FROM msqueue m
GROUP BY staffid
在您的原始查询中,您使用主 select 的列 staffid
的别名将其与子查询的 staffid
列配对。这不足以区分两列,因此您得到了每一行中所有内容的总和。通过在列名前面使用 table 别名 m
和 n
我们现在可以清楚地解决这两个问题
列。
看这里:http://sqlfiddle.com/#!9/ab25a9/1
您甚至可以在子查询中省略 tables 别名,例如
SELECT staffid AS staff,
(SELECT count(*) FROM msqueue WHERE staffid = m.staffid AND feedback =1) AS a,
(SELECT count(*) FROM msqueue WHERE staffid = m.staffid AND feedback =2) AS b,
(SELECT count(*) FROM msqueue WHERE staffid = m.staffid AND feedback =3) AS c,
(SELECT count(*) FROM msqueue WHERE staffid = m.staffid AND feedback =4) AS d
FROM msqueue m
GROUP BY staffid
http://sqlfiddle.com/#!9/ab25a9/2
编辑
在我这边 "clever-mouthing" 之后,我刚刚发现,您的原始陈述有效 - 至少 "on its own" (http://sqlfiddle.com/#!9/ab25a9/4)。我还没有尝试将其放在视图中。也许这就是事情开始出错的时候。更安全的选择肯定是使用 table 别名。
是的,当你把它放在一个视图中时,确实会出错!但是只要外部查询至少有一个 table 别名,一切就OK了:http://sqlfiddle.com/#!9/f5f1e/1
我首先使用 MySql 对其进行了测试,但是语句是相同的,并且在 SQL-服务器,看这里http://sqlfiddle.com/#!6/f5f1e/1
我无法访问 mysql 服务器来对此进行测试,但总的来说 SQL 这可能是一个更简单的解决方案:
SELECT
staffid AS staff,
SUM(CASE WHEN feedback = 1 THEN 1 ELSE 0 END) AS a,
SUM(CASE WHEN feedback = 2 THEN 1 ELSE 0 END) AS b,
SUM(CASE WHEN feedback = 3 THEN 1 ELSE 0 END) AS c,
SUM(CASE WHEN feedback = 4 THEN 1 ELSE 0 END) AS d
FROM
msqueue
GROUP BY
staffid