简体   繁体   English

MySQL右外连接

[英]mySQL right outer join

Im quite new to SQL and am trying to construct a query: 我对SQL很陌生,正在尝试构建查询:

$result = mysql_query("SELECT COUNT(*) AS s_count FROM solution RIGHT OUTER JOIN 
ON offer.oid = solution.oid ". "WHERE offer.is_solved = 0  ORDER BY offer.creation_time 
DESC LIMIT $interval_begin, $interval_end");

The query is supposed to be a liveticker in a shop like environnment: it needs to count all offers that havent been solved and then list them for a certain interval to reflect a certain page of the listing. 该查询应该是环境之类的商店中的动态消息:它需要计算尚未解决的所有报价,然后以一定的间隔列出它们以反映列表的特定页面。 eg if there are 25 offers per age, page 2 would be 26-50. 例如,如果每个年龄段有25个报价,则第2页为26-50。

Can anyone spot why the output is 0? 谁能看出为什么输出为0? All the columns/tables exist and do have test values in them. 所有列/表都存在,并且确实具有测试值。

查询不应该像

mysql_query("SELECT COUNT(*) AS s_count FROM solution RIGHT OUTER JOIN  offer ON offer.oid = solution.oid WHERE offer.is_solved = 0  ORDER BY offer.creation_time  DESC LIMIT".$interval_begin.",".$interval_end); 

looking into your question and your query, I think the most obvious reason(s) why your query always return 0 could be: 调查您的问题和查询,我认为您的查询始终返回0的最明显原因可能是:

  1. The table offer does not have is_solved = 0 for any of its records. 该表offer的任何记录都没有is_solved = 0。 As a result, the WHERE condition offer.is_solved = 0 has no matching entries. 结果,WHERE条件offer.is_solved = 0没有匹配的条目。 Can you please confirm this for your data? 您能为您的数据确认一下吗?

  2. The table offer has no data and thus the RIGHT JOIN yields no results. 该表offer没有数据,因此RIGHT JOIN没有结果。 But as you have mentioned in one of your comments to Rahul's answer, there are 3000 entries in offer , I guess this reason is unlikely. 但是,正如您在对Rahul的回答的评论中提到的那样,有3000项offer ,我想这是不可能的。

Aha, there could be another reason too: are you sure the limit and offset, marked by $interval_begin and $interval_end are correctly set? 啊哈,可能还有另一个原因:确定由$interval_begin$interval_end标记的限制和偏移量是否正确设置? Perhaps if they are both set to 0, the query will always return 0. Can you try doing an "echo/print" of your query and see how does it exactly look? 也许如果它们都设置为0,则查询将始终返回0。您可以尝试对查询执行“回显/打印”操作,看看它的外观如何吗?

Because I have no understanding of your schema, this is only a suggestion, I think that if you have an offer.is_solved column, you do not need a RIGHT JOIN with solution . 因为我不了解您的架构,所以这仅是一个建议,我认为如果您具有offer.is_solved列,则不需要使用solution进行正确的联接。 The following query should work equally fine, no? 以下查询同样可以正常工作,不是吗?

SELECT COUNT(*) AS `s_count`
FROM `offer`
WHERE `offer`.`is_solved` = 0
ORDER BY `offer`.`creation_time`
DESC LIMIT $interval_begin, $interval_end;

Hope it helps! 希望能帮助到你!

What for do you need solution table in your query? 您在查询中需要什么解决方案表? From your text it looks like 从您的文字看起来像

mysql_query("SELECT COUNT(*) FROM offer offer WHERE offer.is_solved = 0  ORDER BY offer.creation_time  DESC LIMIT".$interval_begin.",".$interval_end);

would be enough. 足够了。

"What we need to know is the exact number of solutions for each "offer", not just whether there is any solution at all." “我们需要知道的是每个“要约”的确切解决方案数量,而不仅仅是是否有任何解决方案。” - In such case your initial SQL was wrong, it just counted orders. -在这种情况下,您最初的SQL错误,它只是计算订单数。 You need something like 你需要类似的东西

mysql_query("SELECT offer.oid, COUNT(solution.oid) FROM offer offer LEFT JOIN ON offer.oid = solution.oid WHERE offer.is_solved = 0 GROUP BY offer.oid ORDER BY offer.creation_time  DESC LIMIT".$interval_begin.",".$interval_end);

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

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