简体   繁体   English

从MySQL表中选择数据,其中来自另一个表的某些结果取决于条件(如果有)

[英]Select data from a MySQL table where some results from another table depends on a condition, if there are any

With a PHP code, I wish to order some MySQL results in a specific way. 我希望通过PHP代码以特定方式订购一些MySQL结果。

I have two tables: 我有两个表:

First table, called "post" with the following non-exhaustive content: 第一个表称为“ post”,具有以下非详尽内容:

  • id ID
  • released_date 发布日期
  • published_date 发布日期
  • ... ...

Second table, called "votes" with the following non-exhaustive content: 第二个表称为“投票”,具有以下非详尽内容:

  • id ID
  • post_id post_id
  • user_id 用户身份
  • status 状态

If a user votes for an existing post, a record is made in the "votes" table, inserting the post id as post_id and the user id as user_id. 如果用户对现有帖子投票,则在“投票”表中进行记录,将帖子ID作为post_id插入,将用户ID作为user_id插入。 The status is 1 for an upvote and 2 for a downvote. 状态为1表示赞成,2表示反对。

One important thing to keep in mind though: some posts might never receive any vote at all, and 0 occurrence would be found in the second table, for instance! 但是要记住一件事: 有些帖子可能根本不会获得任何投票,例如,第二张表中出现0个!

So, now on my first page I order my posts with their amount of votes (upvotes-downvotes) the following way, and it works fine : 因此,现在在我的第一页上,我通过以下方式对帖子及其投票数(upvotes-downvotes)进行排序,并且效果很好

$post_req = mysql_query('SELECT * FROM post
WHERE NOW() > released_date
ORDER BY ((SELECT COUNT(*) FROM votes WHERE votes.post_id = post.id AND votes.status = 1)-(SELECT COUNT(*) FROM votes WHERE votes.post_id = post.id AND votes.status = 2)) DESC, published_date DESC
LIMIT 0, 5');

But on the next page (ie a new iteration), I need to continue where I left, in terms of votes. 但是在下一页(即新的迭代)上,我需要从票数上继续我离开的地方。 Before I do that, I save the votes amount of the last post of the first page to pass it to the next one, under the variable $last_post_votes. 在执行此操作之前,我将第一页的最后一个帖子的票数保存到变量$ last_post_votes下,以将其传递给下一个。

Now, this where I struggle: I need to look for posts which have a lower amount of votes than this variable in a new request. 现在,这是我的难题:在新的请求中,我需要查找投票数量低于此变量的帖子。 As follows, my failing attempt: 如下,我失败的尝试:

$post_req_1 = mysql_query('SELECT * FROM post
WHERE NOW() > released_date
AND ((SELECT COUNT(*) FROM votes WHERE votes.post_id = post.id AND votes.status = 1)-(SELECT COUNT(*) FROM votes WHERE votes.post_id = post.id AND votes.status = 2)) < "'.$last_post_votes.'"
ORDER BY ((SELECT COUNT(*) FROM votes WHERE votes.post_id = post.id AND votes.status = 1)-(SELECT COUNT(*) FROM votes WHERE votes.post_id = post.id AND votes.status = 2)) DESC, published_date DESC
LIMIT 5');

And then I tried something like that, which failed too: 然后我尝试了类似的方法,但也失败了:

$post_req_1 = mysql_query('SELECT post.*,
((SELECT COUNT(*) FROM votes WHERE votes.post_id = post.id AND votes.status = 1)-(SELECT COUNT(*) FROM votes WHERE votes.poste_id = post.id AND votes.status = 2)) AS all_votes
FROM post, votes
WHERE NOW() > post.released_date
AND all_votes < "'.$last_post_votes.'"
ORDER BY all_votes DESC, post.published_date DESC
LIMIT 5');

The problem is clearly the condition upon searching results from another table in the SELECT. 问题显然是在SELECT中从另一个表搜索结果时的条件。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

Thanks a lot in advance! 在此先多谢! :-) :-)

Lois 路易

EDIT : 编辑

I managed to make it work as I wished, by using a condition directly in the ORDER BY itself. 通过在ORDER BY本身中直接使用条件,我设法使其按预期工作。 Not sure it's super proper, but it seems to work: 不确定它是否超级合适,但它似乎可以工作:

$post_req_1 = mysql_query('SELECT * FROM post
WHERE NOW() > released_date
ORDER BY ((SELECT COUNT(*) FROM votes WHERE votes.post_id = post.id AND votes.status = 1)-(SELECT COUNT(*) FROM votes WHERE votes.post_id = post.id AND votes.status = 2)) < "'.$last_post_votes_passed.'" DESC, published_date DESC
LIMIT 5');

If your first statement works fine, why don't you continue with that? 如果您的第一句话很好用,为什么不继续呢?

Aou start with the LIMIT clause of LIMIT 0,5 which gives you the first 5 records of the resultset. LIMIT 0,5的LIMIT子句开始,它为您提供结果集的前5条记录。 If you increase the first parameter to LIMIt it will show you the next 5 records, and so on. 如果将第一个参数增加到LIMIt,它将显示接下来的5条记录,依此类推。 No need to fiddle around with interim variables... 无需摆弄临时变量...

// first page
SELECT ... LIMIT 0,5

// seond page
SELECT ... LIMIT 5,5

// 10th page
SELECT ... LIMIT 45,5

See also http://dev.mysql.com/doc/refman/5.0/en/select.html 另请参见http://dev.mysql.com/doc/refman/5.0/en/select.html

If i am not mistaken is this what you are looking for? 如果我没记错的话,这是您要找的东西吗?

SELECT p.id, IFNULL(COUNT(v.id), 0) - IFNULL(COUNT(v2.id), 0) AS `Votes`
FROM
posts p 
LEFT JOIN votes v on v.post_id = p.id AND v.status = 1
LEFT JOIN votes v2 on v2.post_id = p.id and v.status = 2
WHERE NOW() > p.released_date
ORDER BY `Votes` DESC;

暂无
暂无

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

相关问题 从缺少某些WHERE条件的表中选择 - Select from table where some WHERE condition missing MySQL SELECT的结果来自1个表,但排除结果取决于另一个表? - MySQL SELECT results from 1 table, but exclude results depending on another table? 从一个表中选择行,其中基于第二个表ID的另一个第二个表中的ID显示结果的顶部以及下一个结果,因为它是+ Mysql - Select rows from a table where id in another second table based on second table ids shows results top as well as next results as it is + Mysql 从一个条件为的表中检索数据,并在一个SELECT查询中检查另一个表中的记录 - Retrieve data from a table with a condition where and check the record in another table in one SELECT query SELECT表并从另一个表获取一些数据(如果存在) - SELECT table and get some data from another table if they exist 从另一个查询没有返回结果的表中选择所有 - Select all from table where another query retuns no results 来自另一个数据库的MySQL子查询,其中表名取决于主查询 - MySQL subquery from another database where table name depends on main query 如何根据mySQL中另一个表的ID省略选择结果? - How to omit select results based on IDs from another table in mySQL? MySQL我可以从一个表还是另一个表中选择数据? - MySQL Can I Select Data from One table OR another Table? 从一个表中选择计数,另一个表中有一个位置 - Select count from a table with a Where on another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM