mySQL 右外连接
我对 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");
该查询应该是像商店这样的环境中的实时报价机:它需要计算所有尚未解决的报价,然后在一定的时间间隔内列出它们以反映特定的页面列表的。 例如,如果每个年龄有 25 个优惠,第 2 页将是 26-50。
谁能看出为什么输出为0?所有列/表都存在并且其中确实有测试值。
Im quite new to SQL and am trying to construct a query:
$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.
Can anyone spot why the output is 0? All the columns/tables exist and do have test values in them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查询不应该像这样
Shouldn't the query be like
调查您的问题和查询,我认为您的查询始终返回 0 的最明显原因可能是:
表
offer
没有is_solved
= 0 对于其任何记录。因此,WHERE 条件offer.is_solved = 0
没有匹配的条目。您能否确认一下您的数据?表
offer
没有数据,因此RIGHT JOIN不会产生任何结果。但正如您在对 Rahul 的回答的评论之一中提到的,offer
中有 3000 个条目,我想这个原因不太可能。啊哈,可能还有另一个原因:您确定由
$interval_begin
和$interval_end
标记的限制和偏移量设置正确吗?也许如果它们都设置为 0,则查询将始终返回 0。您可以尝试对查询进行“回显/打印”,看看它到底是什么样子吗?因为我不了解您的架构,这只是一个建议,我认为如果您有
offer.is_solved
列,则不需要与solution
进行 RIGHT JOIN 。下面的查询应该同样可以正常工作,不是吗?希望有帮助!
looking into your question and your query, I think the most obvious reason(s) why your query always return 0 could be:
The table
offer
does not haveis_solved
= 0 for any of its records. As a result, the WHERE conditionoffer.is_solved = 0
has no matching entries. Can you please confirm this for your data?The table
offer
has no data and thus the RIGHT JOIN yields no results. But as you have mentioned in one of your comments to Rahul's answer, there are 3000 entries inoffer
, I guess this reason is unlikely.Aha, there could be another reason too: are you sure the limit and offset, marked by
$interval_begin
and$interval_end
are correctly set? 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?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 withsolution
. The following query should work equally fine, no?Hope it helps!
您的查询中需要解决方案表的目的是什么?从你的文字看来
就足够了。
“我们需要知道的是每个“提议”的解决方案的确切数量,而不仅仅是是否有任何解决方案。” - 在这种情况下,你最初的 SQL 是错误的,它只是计算订单。你需要类似的东西
What for do you need solution table in your query? From your text it looks like
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. You need something like