mysql select 不输出_MYSQL选择查询为输出返回NULL的情况(MYSQL Select case when query returns NULL for output)...

博客内容涉及在MySQL中使用CASE WHEN语句对三角形的类型进行分类的问题。作者遇到Isosceles类型的输出返回NULL,问题在于OR条件的表示和条件顺序。解答提供了修正后的SQL查询,减少了嵌套CASE WHEN的使用,提高了查询效率。
摘要由CSDN通过智能技术生成

MYSQL选择查询为输出返回NULL的情况(MYSQL Select case when query returns NULL for output)

在查询时运行嵌套大小写,以根据以下参数对表中的条目进行分类。 当我运行代码时, Isosceles的输出返回Null 。 我不知道为什么。 我正在比较整数a,b和c,它们代表三角形的每个不同整数的列。

有什么建议么?

select case when a+b > c and b+c > a and c+a > b then

case

when a=b|a=c|b=c then "Isosceles"

when a=b and a=c and b=c then "Equilateral"

when a<>b and b<>c and a<>c then "Scalene"

end

else "Not A Triangle"

end

from triangles

Running a nested case when query to classify entries in a table based on the parameters below. When I run the code, the output for Isosceles returns Null. I'm not sure why. I'm comparing integers a,b, and c, which represent columns for each of those distinct integers for a triangle, for instance.

Any suggestions?

select case when a+b > c and b+c > a and c+a > b then

case

when a=b|a=c|b=c then "Isosceles"

when a=b and a=c and b=c then "Equilateral"

when a<>b and b<>c and a<>c then "Scalene"

end

else "Not A Triangle"

end

from triangles

原文:https://stackoverflow.com/questions/43362636

2019-12-17 08:12

满意答案

第一个问题:使用双管||而不是单管 对于OR 。

第二个问题:在Isosceles之前使用Equilateral条件因此,如果你的边是2和2那么根据你的逻辑, Isosceles条件首先满足。 因此,它永远不会将其等同于Equilateral

新答案:事实上,您不需要2个案例陈述。 改用它

select t.*,

case when not (a+b > c and b+c > a and c+a > b)

then "Not A Triangle"

when (a=b and a=c and b=c)

then "Equilateral"

when (a=b or a=c or b=c)

then "Isosceles"

else "Scalene"

end as Triangle_type

from triangle t;

select t.*,

case when a+b > c and b+c > a and c+a > b

then

(case

when a=b and a=c and b=c then "Equilateral"

when a=b or a=c or b=c then "Isosceles"

when a<>b and b<>c and a<>c then "Scalene"

end

)

else

"Not A Triangle"

end as Triangle_type

from triangle234 t

First problem: Instead of single pipe, use double pipe || for OR.

Second problem: Use Equilateral condition before Isosceles So if your sides are 2,2 and 2 then as per your logic, Isosceles condition satisfies first. So it will never equate it to Equilateral

New Answer: In fact, you don't need 2 case statements. Use this instead

select t.*,

case when not (a+b > c and b+c > a and c+a > b)

then "Not A Triangle"

when (a=b and a=c and b=c)

then "Equilateral"

when (a=b or a=c or b=c)

then "Isosceles"

else "Scalene"

end as Triangle_type

from triangle t;

select t.*,

case when a+b > c and b+c > a and c+a > b

then

(case

when a=b and a=c and b=c then "Equilateral"

when a=b or a=c or b=c then "Isosceles"

when a<>b and b<>c and a<>c then "Scalene"

end

)

else

"Not A Triangle"

end as Triangle_type

from triangle234 t

2017-04-12

相关问答

在参考上述编辑之后,确定如下,这里是解决方案 使用通配符“%”时使用“LIKE”而不是“=” 所以你现在的查询应该是 $queryText = "SELECT * FROM tags WHERE tag LIKE '%" . $search . "%'";

[我在本地系统上创建了完全相同的数据库,并运行相同的代码,完成上述更改后,它按预期运行] Ok so after referring to the above edit you made, here is the solution Use "...

当你调用fetch_assoc() ,你忘记了括号: while ($row=$result5->fetch_assoc) {

应该: while ($row=$result5->fetch_assoc()) {

因此,永远不会输入循环,并且未设置$uid和$visits (因此它们仍然为null )。 When you're calling fetch_assoc(), you're forgetting the parentheses: while ($row=$result5->fetc...

SELECT

COALESCE(t.MemoryUsage, t2.MemoryUsage),

COALESCE(t.TransactionTime, t2.TransactionTime),

COALESCE(t.moduleid, t2.moduleid)

FROM (

SELECT *

FROM tbl_trnmemoryusage

ORDER BY TransactionTime DESC

LIMIT 1

) t2

LEFT JOIN (

SELEC...

你应该使用左外连接。 SELECT SUM(a.quantity - coalesce(b.quantity, 0)) AS stock

FROM wp_wpsp_inventory_items a

LEFT JOIN

wp_wpsp_assigned_inventory b

ON a.master_id = b.master_id

WHERE a.master_id = '9'

You should use left outer join. SELECT SUM(a.quantity - c...

我想你想要 , CASE WHEN csc.NAME LIKE ('Microsoft Office%') AND csc.NAME NOT IN ('Microsoft Office Shortcut Bar 2000','Office 2011 for Mac') THEN csc.NAME ELSE '-' END AS sw_microsoft_office

I guess you want , CASE WHEN csc.NAME LIKE ('Microsoft Office%') ...

你不能在纯sql中这样做,因为查询是作为一个整体进行分析的,包括SELECT COUNT(*) FROM SYS."A"因此,如果表SYS."A"不存在,那么整个查询将无法解析。 另一种方法是使用pl / sql。 如果表存在,并且不存在,则创建一个计数行的函数 - 返回null。 考虑下面的例子: create or replace function get_table_count(sTableName varchar2)

return number

is

iCount number;

be...

mysql_query的签名只接受查询字符串...您不需要传递连接,因为它是在后台设置的。 正在调用die,因为函数调用无效,这就是错误为空的原因,因为查询从未运行过。 是的,不推荐使用mysql_query以支持mysqli 。 你不妨从一开始就学习它,这样你就不必学习两次了。 The signature for mysql_query only takes the query string... you don't need to pass the connection because tha...

第一个问题:使用双管||而不是单管 对于OR 。 第二个问题:在Isosceles之前使用Equilateral条件因此,如果你的边是2和2那么根据你的逻辑, Isosceles条件首先满足。 因此,它永远不会将其等同于Equilateral 新答案:事实上,您不需要2个案例陈述。 改用它 http://rextester.com/PTDI48631 select t.*,

case when not (a+b > c and b+c > a and c+a > b)

then...

创建一个只有一行的表。 然后,您可以使用左连接来实现所需的NULL结果。 CREATE TABLE dummy (d TINYINT NOT NULL);

INSERT INTO dummy SET d = 1;

SELECT q25,

( ( AVG( q1 ) + AVG( q2 ) + AVG( q3 ) ) /3 ) AS Overall

FROM dummy LEFT JOIN t_results

ON brand = 'XYZ'

AND DATE = 'MAY20...

ID永远不会为null,因为不返回任何行,并且永远不会执行CASE WHEN。 解决方法是这样的: SELECT COALESCE(MAX(ID), 1) AS ID

FROM (

SELECT ID

FROM TableA

WHERE ColumnA = 'G'

ORDER BY Id DESC

LIMIT 1

) s

(聚合查询将始终返回一行,如果返回ID,则返回MAX(ID);如果子查询没有返回任何行,则返回null) The ID is never null bec...

相关文章

查询一个对象(实体类必须有一个不带参数的构造方法),使用select查询,基于投影的查询,通过在列表中

...

研究 Hadoop 0.21代码时,很多情况下需要运行单个test case,如果新增了功能要测试

...

原文出处:http://blog.chenlb.com/2010/08/solr-use-custom

...

原文出处:http://blog.chenlb.com/2010/08/solr-use-custom

...

我想在进入系统首页的时候就对数据库进行查询,然后讲要查询的数据以下拉里表的形式放在页面上 Actio

...

package com.webchart.dao; import java.sql.SQLExcep

...

从数据库查询N条记录放在List集合中,然后通过request对象返回给页面,通过循环遍历将List中

...

查询操作符(Query Operators)可以让我们写出复杂查询条件,让我们使用的过程更加灵活。官方

...

请问各位: 我在使用junit进行单元测试的时候,到实例化SessionFactory的时候不成功,

...

最新问答

如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re

第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型

这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;

问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是

我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar

Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/

你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV

12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar

这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值