简体   繁体   English

Mysql根据类似计数从多个表中选择

[英]Mysql select from multiple tables based on like count

I have this problem with my sql statement and am not sure how to get around it. 我的sql语句有这个问题,我不知道如何绕过它。 I am using WordPress and the WTI Like Post plugin Here is the query: 我正在使用WordPress和WTI Like Post插件以下是查询:

 SELECT wpblog_posts.ID, wpblog_posts.post_title, wpblog_posts.post_excerpt, wpblog_posts.guid, wpblog_posts.post_author, wpblog_wti_like_post.value, wpblog_wti_like_post.post_id 
 FROM wpblog_posts, wpblog_wti_like_post 
 LEFT JOIN wpblog_term_relationships rel ON rel.object_id = wpblog_posts.ID 
 LEFT JOIN wpblog_term_taxonomy tax ON tax.term_taxonomy_id = rel.term_taxonomy_id 
 LEFT JOIN wpblog_terms t ON t.term_id = tax.term_id WHERE t.term_id = 165 
 AND wpblog_posts.post_type = 'post' 
 AND wpblog_wti_like_post.post_id = wpblog_posts.ID 
 ORDER BY wpblog_wti_like_post.value 
 LIMIT 20 

Here is a screen shot of the wpblog_wti_like_post table: 这是wpblog_wti_like_post表的屏幕截图: 在此输入图像描述

So what i'm trying to do is select all posts from a category, and order those posts by wpblog_wti_like_post.value Now in the category some posts are liked and disliked, and appear in the table wpblog_wti_like_post but some posts don't appear in that table. 所以我要做的是选择一个类别中的所有帖子,并通过wpblog_wti_like_post.value订购这些帖子现在在该类别中,一些帖子是喜欢和不喜欢的,并且出现在表wpblog_wti_like_post但是有些帖子没有出现在表。

How would I: 我怎么会:

a. 一种。 Select all posts in that category 选择该类别中的所有帖子

b. Order by wpblog_wti_like_post.value 按wpblog_wti_like_post.value排序

I am completely stumped on how to accomplish this. 我完全不知道如何实现这一目标。

Cheers 干杯

UPDATE UPDATE

This query selects all posts from a specific category and works fine, but the query above doesn't: 此查询选择来自特定类别的所有帖子并且工作正常,但上面的查询不会:

SELECT wpblog_posts.ID, wpblog_posts.post_title, wpblog_posts.post_excerpt, wpblog_posts.guid, wpblog_posts.post_author 
FROM wpblog_posts 
LEFT JOIN wpblog_term_relationships rel ON rel.object_id = wpblog_posts.ID 
LEFT JOIN wpblog_term_taxonomy tax ON tax.term_taxonomy_id = rel.term_taxonomy_id 
LEFT JOIN wpblog_terms t ON t.term_id = tax.term_id 
WHERE t.term_id = 165 
LIMIT 20 

Tip: something like the following is more readable... 提示:类似以下内容更具可读性......

SELECT p.ID
     , p.post_title
     , p.post_excerpt
     , p.guid
     , p.post_author
  FROM wpblog_posts p
  LEFT 
  JOIN wpblog_term_relationships rel 
    ON rel.object_id = p.ID
  LEFT 
  JOIN wpblog_term_taxonomy tax 
    ON tax.term_taxonomy_id = rel.term_taxonomy_id 
  LEFT 
  JOIN wpblog_terms t 
    ON t.term_id = tax.term_id
 WHERE t.term_id = 165
 LIMIT 20 

Some observations on the above: 关于上述的一些观察:

  • LEFT JOIN x... WHERE x = is the same as INNER JOIN x LEFT JOIN x... WHERE x =INNER JOIN x相同

  • There is no point LEFT JOINing tables from which you select no columns. LEFT JOINing表没有选择没有列的表。 As stated, the above is in fact an inner join, so this fact is for future reference only. 如上所述,上述内容实际上是内连接,因此这个事实仅供将来参考。

  • LIMIT without ORDER BY is fairly meaningless 没有ORDER BY的LIMIT是没有意义的

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

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