[英]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”,具有以下非详尽内容:
Second table, called "votes" with the following non-exhaustive content: 第二个表称为“投票”,具有以下非详尽内容:
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.