简体   繁体   中英

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

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

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).

The second query output will be the distinct values instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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