简体   繁体   English

MySQL查询中答案为true,false

[英]Mysql query in with the answer true, false

I was not too long ago studying java, so maybe I have a stupid question. 我不久以前学习Java,所以也许我有一个愚蠢的问题。

I have a table, where i store some info. 我有一张桌子,我在那里存储一些信息。 And i want to check in this table on availability of some information (it will be only one line), and if yes - take from this line, the information in a particular column, if no - do another thing. 我想在此表中检查某些信息的可用性(仅一行),如果是,请从该行中获取特定列中的信息,如果不行,请执行其他操作。

I really don`t know how to do this. 我真的不知道该怎么做。 My idea was something like this: at first - check the table: 我的想法是这样的:首先-检查表:

SELECT * FROM tablename WHERE nickname = "kvant";

then if true, do another query with searching info. 然后如果为true,请使用搜索信息进行另一个查询。 and do this with condition if\\else. 并在条件\\ else下执行此操作。 but all my attempts not turn. 但是我所有的尝试都没有结束。

I hope for your help, sorry for my awry English. 希望对您有所帮助,对不起我的英语。

Check if the data exists like this: 检查数据是否存在,如下所示:

IF EXISTS(SELECT * FROM tablename WHERE nickname = "kvant")
BEGIN
    --value is found so go ahead 
END
ELSE
    --value not found
BEGIN
END

One simple way is to query the database and get the result. 一种简单的方法是查询数据库并获取结果。 If result!=Null {//do something} else {//Do something}

(Can use try - catch as well) (可以使用try - catch

Pure java possible solution: 纯java可能的解决方案:

Try to get that value in the query. 尝试在查询中获取该值。 Get the ResultSet rs of that query and: 获取该查询的ResultSet rs并:

if(rs.next()) {
    String value = rs.getString("value"); //Assuming the column is value and that it is a String.
    //Do whatever you want with the value
} else {
    //The other thing
}

If you can not take the value from the first ResultSet, do another query in the if. 如果您不能从第一个ResultSet中获取值,请在if中进行另一个查询。

write in sql like select case exists (select * from yourtable where condition) then 'dothis thing' else 'dootherthing' from 'yourtable' where 'yourcondition' 以类似select case exists (select * from yourtable where condition) then 'dothis thing' else 'dootherthing' from 'yourtable' where 'yourcondition'写在sql中select case exists (select * from yourtable where condition) then 'dothis thing' else 'dootherthing' from 'yourtable' where 'yourcondition'

since you wanted to run some other query after you know you have some conditon exists. 因为您想知道存在一些条件后就想运行其他查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM