这是我的观点,它提供了如上所述的输出:
CREATE
ALGORITHM=UNDEFINED
DEFINER=`root`@`localhost`
SQL SECURITY DEFINER
VIEW new_view AS
SELECT 'pcount' AS ind_type,
SUM(pcount) AS Index_val
FROM temp
UNION
SELECT 'ncount',
SUM(ncount)
FROM temp
;
结果:
+----------+-----------+ | ind_type | Index_val | +----------+-----------+ | pcount | 76 | | ncount | 434 | +----------+-----------+
现在我创建了另一个使用pcount
ncount
和MAX()
的密钥
CREATE
ALGORITHM=UNDEFINED
DEFINER=`root`@`localhost`
SQL SECURITY DEFINER
VIEW view_name AS
SELECT pcount,
ncount
FROM temp
WHERE id = (SELECT MAX(id) FROM temp)
;
给出结果:
+--------+--------+ | pcount | ncount | +--------+--------+ | 56 | 56 | +--------+--------+
如何将此结果视为上述结果?
按照斯蒂芬的回答结果:
+----------+-----------+
| ind_type | Index_val |
+----------+-----------+
| 0 | 0 |
+----------+-----------+
1 row in set (0.01 sec)
答案 0 :(得分:1)
你可以试试这个:
CREATE VIEW test.t1 AS (
SELECT
'pcount' AS type,
`temp`.`pcount` AS value
FROM
`temp`
WHERE (
`temp`.`id` = (
SELECT
MAX(`temp`.`id`)
FROM
`temp`
)
)
UNION
SELECT
'ncount' AS type,
`temp`.`ncount` AS value
FROM
`temp`
WHERE (
`temp`.`id` = (
SELECT
MAX(`temp`.`id`)
FROM
`temp`
)
)
)
答案 1 :(得分:1)
您可以使用由两行组成的虚拟表(根据您要忽略的列数)在第二个示例中取消忽略查询结果。这是它的外观:
SELECT t.ind_type,
CASE t.ind_type
WHEN 'pcount' THEN v.pcount
ELSE v.ncount
END AS Index_val
FROM temp AS v
CROSS
JOIN (
SELECT 'pcount' AS ind_type
UNION ALL
SELECT 'ncount'
) AS t
WHERE v.id = (SELECT MAX(id) FROM temp)