[英]Multiple selects in one query in java with jdbc connector-Mysql
我尝试在Java中进行多次选择。 我有两个字符串“ name”和“ artist”,即使其中之一为null,我也要在一个查询中同时选择它们。
我做了这样的事情:
if ( !nameIsEmpty && !artisIsEmpty )
{
rst = stmt.executeQuery("SELECT * FROM school.product_table where name=" + "'" + name + "' and artist=" + "'" + artist + "'");
}
else if ( nameIsEmpty && !artisIsEmpty )
{
rst = stmt.executeQuery("SELECT * FROM school.product_table where artist=" + "'" + artist + "'");
}
else if ( !nameIsEmpty && artisIsEmpty )
{
rst = stmt.executeQuery("SELECT * FROM school.product_table where name=" + "'" + name + "'");
}
else
{
productIsEmpty = true;
}
我认为这不是最好的方法。 我希望在一个查询中有一种简便的方法。 提前致谢。
SELECT * FROM school.product_table其中name在(name.null)中,艺术家在(name,null)中
除了将记录与名称和艺术家匹配外,它还将返回名称为空和艺术家的艺术家,艺术家为空和名称,null和null的结果。
如果您想同时跳过两个空结果,请尝试以下操作-
SELECT * FROM school.product_table其中name(null)中的名称和(name,null)中的艺术家和!(artist = null && name = null)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.