简体   繁体   English

来自不同计数的结果不同,并选择不同查询

[英]Different results from distinct count and select distinct queries

SELECT distinct COUNT(s.title) as total_tags 
  FROM products s 
 INNER JOIN products_vt vt on vt.pruduct_id=s.id 
 where s.deleted!=1 
   AND vt.positives<5 
   AND (s.title like '%neon pictures free screensaver%' 
         OR s.title LIKE 'neon%' 
         OR s.title LIKE '%neon' 
         OR s.title LIKE '%neon%' 
         OR s.title LIKE 'pictures%' 
         OR s.title LIKE '%pictures' 
         OR s.title LIKE '%pictures%' 
         OR s.title LIKE 'free%' 
         OR s.title LIKE '%free' 
         OR s.title LIKE '%free%' 
         OR s.title LIKE 'screensaver%' 
         OR s.title LIKE '%screensaver' 
         OR s.title LIKE '%screensaver%' ) 
 ORDER by s.title like '%neon pictures free screensaver%' desc , 
          s.title LIKE 'neon%' desc , 
          s.title LIKE '%neon' desc , 
          s.title LIKE '%neon%' desc , 
          s.title LIKE 'pictures%' desc , 
          s.title LIKE '%pictures' desc , 
          s.title LIKE '%pictures%' desc , 
          s.title LIKE 'free%' desc , 
          s.title LIKE '%free' desc , 
          s.title LIKE '%free%' desc , 
          s.title LIKE 'screensaver%' desc , 
          s.title LIKE '%screensaver' desc , 
          s.title LIKE '%screensaver%' desc

Result: 2549 结果:2549

SELECT distinct(s.title), 
       s.date_updated, 
       s.title, 
       s.id, 
       s.icon, 
       s.downloads, 
       s.date, 
       s.date_updated, 
       s.version, 
       s.description80, 
       s.downloads,
       s.views,
       s.type, 
       s.platform, 
       s.rating_users,
       vt.positives, 
       vt.total, 
       vt.permalink, 
       vt.scan_date, 
       s.keywords 
  FROM products s 
 INNER JOIN products_vt vt on vt.pruduct_id=s.id 
 where s.deleted!=1 
   AND vt.positives<5 
   AND (s.title like '%neon pictures free screensaver%' 
         OR s.title LIKE 'neon%' 
         OR s.title LIKE '%neon' 
         OR s.title LIKE '%neon%' 
         OR s.title LIKE 'pictures%' 
         OR s.title LIKE '%pictures' 
         OR s.title LIKE '%pictures%' 
         OR s.title LIKE 'free%' 
         OR s.title LIKE '%free' 
         OR s.title LIKE '%free%' 
         OR s.title LIKE 'screensaver%' 
         OR s.title LIKE '%screensaver' 
         OR s.title LIKE '%screensaver%' ) 
 ORDER by s.title like '%neon pictures free screensaver%' desc , 
          s.title LIKE 'neon%' desc , 
          s.title LIKE '%neon' desc , 
          s.title LIKE '%neon%' desc , 
          s.title LIKE 'pictures%' desc , 
          s.title LIKE '%pictures' desc , 
          s.title LIKE '%pictures%' desc , 
          s.title LIKE 'free%' desc , 
          s.title LIKE '%free' desc , 
          s.title LIKE '%free%' desc , 
          s.title LIKE 'screensaver%' desc , 
          s.title LIKE '%screensaver' desc , 
          s.title LIKE '%screensaver%' desc

Result: 2492 结果:2492

I don't think either query is structured correctly. 我认为任何查询的结构都不正确。 If you want the count of distinct titles then the first query should be: 如果要计算不同标题的数量,则第一个查询应为:

SELECT COUNT(DISTINCT s.title) as total_tags...

If you want a list of distinct titles then you need to eliminate the other rows from the query: 如果要获得不同标题的列表,则需要从查询中消除其他行:

SELECT DISTINCT s.title as total_tags...

A query like the first one will count all the records and give output, and it won't give count for distinct values. 像第一个查询这样的查询将对所有记录进行计数并给出输出,而不会对不同的值进行计数。 SELECT distinct COUNT(s.title) (the first operation will be the count, then distinct). SELECT distinct COUNT(s.title) (第一个操作是计数,然后不重复)。

The second query output will be the distinct values instead. 第二个查询输出将是不同的值。

感谢您的回答,我通过在第二个查询中使用GROUP BY而不是SELECT DISTINCT来解决了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM