我尝试在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;
}
我认为这不是最好的方法。我希望有一种简单的方法可以在一次查询中实现这一点。 提前致谢。
最佳答案
从 school.product_table 中选择 *,其中名称为 (name.null),艺术家为 (name,null)
除了匹配姓名和艺术家的记录外,它还会返回 name=null 和艺术家、artist=null 和 name、null 和 null 的结果。
如果您想跳过两个空结果,请尝试此 -
从 school.product_table 中选择 *,其中名称为 (name.null),艺术家为 (name,null) 和 !(artist=null && name=null)
关于java - 使用jdbc连接器在java中的一个查询中进行多个选择-Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21066890/