帮助我进行 SQL 查询(需要它始终返回至少一行。)

发布于 2024-09-13 05:36:22 字数 1290 浏览 6 评论 0原文

首先,这是我的 SQL 查询:

SELECT research_cost, tech_name,
    (SELECT research_cost
        FROM technologies
        WHERE research_cost <= USERS_RESEARCH_POINTS_VALUE
        ORDER BY research_cost DESC
        LIMIT 1) as research_prev,
    (SELECT cost
        FROM technology_costs
        WHERE id = 18
        LIMIT 1) as technology_cost
FROM `technologies`
    JOIN technology_costs
    ON id = COUNT_OF_TECHS_USER_LEARNED
WHERE research_cost > USERS_RESEARCH_POINTS_VALUE
ORDER BY research_cost ASC
LIMIT 1

网站链接: http://www.joemajewski.com/fortress/

有些人可能认为上面的查询是中等大小的,甚至很小;不是我。对我来说,这是我写过的最大、最复杂的 SQL 查询之一,所以我挑战了自己的极限。

无论如何,查询本身没有语法错误,并且在大多数情况下它完全按照我想要的方式执行。不过,还有一个小问题。

如果主 SELECT 子句不返回行(“SELECT Research_cost, tech_name FROM technologies WHERE Research_cost > Whatever”),则整个查询是一个空集。

如果有帮助的话,《堡垒》就是我正在开发的这款游戏。这是一款基于浏览器的大型多人在线角色扮演游戏,用户可以组建一支军队,尝试在排行榜上名列前茅。技术是一种游戏机制,用户可以通过购买升级来升级自己的统计数据(金币收入、单位产量等)。研究是一种游戏机制,可让您通过培训科学家进行研究来学习新技术。这就是这个查询的全部内容。它抓住了当前正在研究的技术及其研究成本。它还抓住了下一代技术的成本的黄金成本。

那么...有什么方法可以强制在结果集中至少返回一个空行,因为嵌套选择语句检索我在脚本中需要的变量,即使外部查询什么也不返回。或者有人可能会改变它以便始终返回结果。

如果还需要什么,请告诉我。 :)

First off, here's my SQL query:

SELECT research_cost, tech_name,
    (SELECT research_cost
        FROM technologies
        WHERE research_cost <= USERS_RESEARCH_POINTS_VALUE
        ORDER BY research_cost DESC
        LIMIT 1) as research_prev,
    (SELECT cost
        FROM technology_costs
        WHERE id = 18
        LIMIT 1) as technology_cost
FROM `technologies`
    JOIN technology_costs
    ON id = COUNT_OF_TECHS_USER_LEARNED
WHERE research_cost > USERS_RESEARCH_POINTS_VALUE
ORDER BY research_cost ASC
LIMIT 1

Website link: http://www.joemajewski.com/fortress/

Some people might consider the above query to be moderate-sized, or even small; not me. For me, that's one of the largest and most complex SQL queries I've ever written, so I pushed myself to my limits.

Anyways, the query itself has no syntax errors, and it does exactly what I want it to do, for the most part. There's one minor issue, however.

If the main SELECT clause doesn't return a row (the "SELECT research_cost, tech_name FROM technologies WHERE research_cost > whatever"), then the entire query is an empty set.

If it helps, Fortress is this game that I'm working on. It's a browser-based MMORPG where users build an army to try and get a top stop in the leaderboards. Technologies are a game mechanic where users purchase upgrades to their stats (gold income, unit production, etc). Researching is a game mechanic that allows you to learn new technologies by training scientists to do the researching. That's what this query is all about. It grabs the current technology that is being researched, as well as the research cost of it. It additionally grabs the cost in gold of how much the next technology will cost.

So... is there any way that I can force at least a null row to be returned in the result set, as the nested select statements retrieve variables that I need in the script, even if the outer query returns nothing. Or could someone possibly change it around so that a result is always returned.

If anything else is needed, let me know. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

昔梦 2024-09-20 05:36:22

此查询始终返回至少 1 行

SELECT * FROM (__your old query__)aaa
UNION 
SELECT NULL as research_cost, NULL as tech_name, NULL as research_prev, 
... etc [FROM DUAL] // DUAL is a  dummy table in mysql

This query always returns at least 1 row

SELECT * FROM (__your old query__)aaa
UNION 
SELECT NULL as research_cost, NULL as tech_name, NULL as research_prev, 
... etc [FROM DUAL] // DUAL is a  dummy table in mysql
┈┾☆殇 2024-09-20 05:36:22

一种不需要列出每个原始列的第二个选择的方法:

SELECT yourstuff.*
FROM (__your original query__) yourstuff
RIGHT OUTER JOIN (SELECT 1 AS fake FROM DUAL) faketable ON 1=1;

如果原始查询返回任何行,它们将不受影响地传递。如果它没有返回任何行,您将得到一行全部为空的行。

An approach that does not require a second select that lists every original column:

SELECT yourstuff.*
FROM (__your original query__) yourstuff
RIGHT OUTER JOIN (SELECT 1 AS fake FROM DUAL) faketable ON 1=1;

If your original query returns any rows, they are passed through unaffected. If it returns no rows, you will get one row of all nulls.

温柔戏命师 2024-09-20 05:36:22

我会尝试将它包装在 ISNULL() 中,

如下所示:

SELECT research_cost, tech_name, 
ISNULL((SELECT research_cost 
    FROM technologies 
    WHERE research_cost <= USERS_RESEARCH_POINTS_VALUE 
    ORDER BY research_cost DESC 
    LIMIT 1), 0.00) as research_prev, 
    [the rest of your query here]

//Note: instead of '0.00' you'll want some appropriate default value.

I would try wrapping it in ISNULL()

Like this:

SELECT research_cost, tech_name, 
ISNULL((SELECT research_cost 
    FROM technologies 
    WHERE research_cost <= USERS_RESEARCH_POINTS_VALUE 
    ORDER BY research_cost DESC 
    LIMIT 1), 0.00) as research_prev, 
    [the rest of your query here]

//Note: instead of '0.00' you'll want some appropriate default value.
停滞 2024-09-20 05:36:22

您可以尝试首先使用正确的架构获取一个空行,然后尝试使用上面编写的查询填充这些行。唯一的缺点是执行此操作需要额外的查询。

这样,如果上面的查询没有返回任何内容,则仍然有空行,您可以检查它。

You can try getting an empty row with the proper schema first, then try filling those rows in with the query you wrote above. The only downside is the additional query to do so.

That way, if your query above returns nothing, you still have the empty row and you can check on that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文