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

时间:2016-04-17 14:14:09

标签: php mysql

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
  

结果: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
  

结果:2492

3 个答案:

答案 0 :(得分:1)

我认为这两种查询都没有正确构建。如果你想要不同标题的计数,那么第一个查询应该是:

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

如果您需要不同标题的列表,则需要从查询中删除其他行:

SELECT DISTINCT s.title as total_tags...

答案 1 :(得分:0)

像第一个查询一样的查询将计算所有记录并给出输出,并且它不会计算不同的值。 SELECT distinct COUNT(s.title) (第一个操作将是计数,然后是不同的。)

第二个查询输出将是不同的值。

答案 2 :(得分:0)

感谢您的回答,我在第二次查询中使用GROUP BY代替SELECT DISTINCT解决了这个问题。