我有以下两个问题。一个是获取实际数据,另一个是计算总共有多少结果,因为我使用代码点火器的分页来分解结果。为什么这些不同,如何确保它们返回相同数量的结果?
查询1:
SELECT DISTINCT `t`.`TermID`, `t`.`Definition`, `t`.`Name` FROM `term` AS `t`
JOIN `syllabusin` AS `si` ON `t`.`TermID` = `si`.`TermID`
JOIN `synonyms` AS `sy` ON `t`.`TermID` = `sy`.`TermID`
WHERE (`si`.`SyllabusID` = 7
OR `si`.`SyllabusID` = 6 )
AND (`t`.`Name` LIKE '%portability testing%'
OR `t`.`Acronym` LIKE '%portability testing%'
OR `t`.`Definition` LIKE '%portability testing%' )
LIMIT 20 OFFSET 0
查询2:
SELECT DISTINCT Count(*) as count FROM `term` AS `t`
JOIN `syllabusin` AS `si` ON `t`.`TermID` = `si`.`TermID`
JOIN `synonyms` AS `sy` ON `t`.`TermID` = `sy`.`TermID`
WHERE (`si`.`SyllabusID` = 7
OR `si`.`SyllabusID` = 6 )
AND (`t`.`Name` LIKE '%portability testing%'
OR `t`.`Acronym` LIKE '%portability testing%'
OR `t`.`Definition` LIKE '%portability testing%' )
正如您将能够看到唯一的区别是' LIMIT 20 OFFSET 0'和COUNT(*)
答案 0 :(得分:2)
SELECT COUNT(DISTINCT column_name) FROM table_name;
与SELECT DISTINCT COUNT(column_name) FROM table_name;
试试这个:
SELECT Count(DISTINCT `t`.`TermID`, `t`.`Definition`, `t`.`Name`) as count FROM `term` AS `t` ...
而不是:
SELECT DISTINCT Count(*) as count FROM `term` AS `t` ...
希望它有用!
答案 1 :(得分:2)
SELECT DISTINCT Count(*) as count FROM `term` AS `t`
应该是
SELECT Count(DISTINCT `t`.`TermID`) as count FROM `term` AS `t`
答案 2 :(得分:0)
distinct有助于在列中获取不同的行和不同的值......第一个查询是在表的行中查找列termID,Definition,name的不同值并显示它们。 ..........而第二个查询使用带有distinct的count函数,它计算表中存在的不同行的数量....所以这两个查询看起来是相同的但是生成了两个不同的价值................................