我只是 SQL 的初学者,所以请不要担心 :)
我有存储字典单词翻译的表。该表有列id
,la
(语言),lb
(目标语言),wa
(原始词),wb
(翻译词)和一些更无关紧要的列:)。我想显示一个表格概述,其中将列出所有语言的列表,其中包含每种语言中的字数加上以 SUM 作为总字数的语言的行(所有仅不同单词的计数)。
我写了这个查询:
SELECT `lng`, COUNT(`word`) AS `count`
FROM (
SELECT DISTINCT *
FROM (
SELECT DISTINCT `la` AS `lng`, `wa` AS `word`
FROM `dict_trans`
UNION ALL
SELECT DISTINCT `lb` AS `lng`, `wb` AS `word`
FROM `dict_trans`
) AS `tbla`
) AS `tblb` GROUP BY `lng`
UNION ALL
SELECT 'sum' AS `lng`, COUNT(`word`) AS `count`
FROM (
SELECT DISTINCT *
FROM (
SELECT DISTINCT `la` AS `lng`, `wa` AS `word`
FROM `dict_trans`
UNION ALL
SELECT DISTINCT `lb` AS `lng`, `wb` AS `word`
FROM `dict_trans`
) AS `tblc`
) AS `tbld` ORDER BY `count` DESC
但我认为多做一个子查询非常愚蠢且性能不友好。
SELECT DISTINCT *
FROM (
SELECT DISTINCT `la` AS `lng`, `wa` AS `word`
FROM `dict_trans`
UNION ALL
SELECT DISTINCT `lb` AS `lng`, `wb` AS `word`
FROM `dict_trans`
) AS `tbla`
我尝试在代码的第二部分从第一部分传递对表的引用:
SELECT `lng`, COUNT(`word`) AS `count`
FROM (
SELECT DISTINCT *
FROM (
SELECT DISTINCT `la` AS `lng`, `wa` AS `word`
FROM `dict_trans`
UNION ALL
SELECT DISTINCT `lb` AS `lng`, `wb` AS `word`
FROM `dict_trans`
) AS `tbla`
) AS `tblb` GROUP BY `lng`
UNION ALL
SELECT 'sum' AS `lng`, COUNT(`word`) AS `count`
FROM `tblb`
ORDER BY `count` DESC
但是抛出了错误(#1146 - 表 'db.tblb' 不存在)。
不创建临时表就可以解决这个问题吗?