I try to multiple select in java. I have two string "name" and "artist" i want to select them both in one query even if one of them is null .
I did something like that :
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;
}
I think its not the best way to do this. And I hope there is a easy way to this in one query. Thanks in advance.
SELECT * FROM school.product_table where name in(name.null) and artist in (name,null)
Along with matching the records with name and artist, it will also return results with name=null and artist, artist=null and name, null and null.
If you want to skip both null results try this -
SELECT * FROM school.product_table where name in(name.null) and artist in (name,null) and !(artist=null && name=null)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.