如果条件为true,如何选择一行,如果在sql中条件为false,则如何选择所有行?

时间:2016-05-16 17:20:16

标签: mysql sql sql-server relational-database

假设有一个表

临时

col1 | col ----

100 | ç

456 | C1

131 | c2

-

假设我有coll1 = 100表示​​有效行查询必须只返回行
如果表中不存在值,则查询必须返回表中的所有行。

注意:我们不能使用if exists或任何程序事物(只有SQL没有TSQL)

我们不能在下面使用

   if exists( select 1 from temp where col1=100)

      begin
         select 1 from temp where col1=100 
     end
  else 
     begin
        select  * from temp where col1=100 
     end

2 个答案:

答案 0 :(得分:1)

$qry1="Select Col1 from table where col1=100";
$res1 = mysql_query($qry1);
if(row_count($res)>0){
/* Process data*/
}
else{
$qry2="Select * from table where col1=100";
$res2 = mysql_query($qry2);
/*process data*/
}

答案 1 :(得分:0)

只需使用public void doBattle(Monster enemy) { //boolean fled = false; //Greets player into battle by telling what monster they are fighting text.appendText("\n" + "A wild " + enemy.getName() + " has appeared!" + "\n" ); mobImagePane.setImage(enemy.getImage()); //System.out.print("THIS RAN"); //debug while ( p.getHealth() > 0 && enemy.getHealth() > 0 ) //while enemy and player are alive { //Prompts user to attack or run text.appendText("Attack " + enemy.getName() + "? (Y/N) " + "\n"); inputText.setOnAction(event -> { String fightChoice = inputText.getText(); fightChoice = fightChoice.toUpperCase(); //String fightChoice = this.choice; if (fightChoice.equals("Y"))//if they want to fight { //Player turn enemy.setHealth(enemy.getHealth() - p.getDamage()); //Sets the monsters health as their current health minus the players damage text.appendText("You attack " + enemy.getName() + " for " + p.getDamage() + " damage!" + "\n" + "Enemy health is " + enemy.getHealth() + "\n"); //Monster turn p.setHealth(p.getHealth() - enemy.getDamage()); //Sets the players health as their current health minus the monsters damage text.appendText("The " + enemy.getName() + " hit you for " + enemy.getDamage() + " damage!" + "\n" + "Your health is " + p.getHealth() + "\n"); //prints how much damage the monster does to the player if (p.health < 20.0) { text.appendText("Your health is low, you should return home and restore health!" + "\n"); } //checks if the player or monster is dead this.checkLife(); enemy.checkLife(); } else { if (fightChoice.equals("N")) // if they don't want to fight { mobImagePane.setImage(null); text.appendText("You fled from the fight!" + "\n"); this.setNewLoc("TOWN"); // brings you back to town fled = true; //JOptionPane.showMessageDialog(null, "You are now in " + this.currentLoc.getName() + "\n" + this.currentLoc.getDescription()); //String move2 = JOptionPane.showInputDialog(null,"Which way do you want to go? (N, E, S, W)"); //makeMove(move2); //break; } else // they don't make any sense { text.appendText("Unrecognized command" + "\n"); } } //} //when someone dies if (!fled) { if (p.alive) // if you are still standing { //print results (money earned, health remaining) mobImagePane.setImage(null); p.wallet += enemy.getLoot(); playerInfo.setText(p.getPlayerName() + "\n" + "Health: " + p.getHealth() + "\n" + "Wallet: " + p.getWallet() + "\n"); text.setText("You shrekt the " + enemy.getName() + "\n" + "You got $" + enemy.getLoot() + " for winning!" + "\n" + "You now have $" + p.wallet + "\nYour health is " + p.getHealth() + "\n"); } else //if you died { mobImagePane.setImage(null); text.setText("You have been shrekt by the " + enemy.getName() + "\n" + "GAME OVER" + "\n"); text.appendText("\nPlay again? (Y/N)" + "\n"); inputText.setOnAction(event2 -> { String answer = inputText.getText(); answer.toUpperCase(); //String answer = this.choice; if (answer.equals("Y")) //if they want to play again { text.appendText("Alright! Let's go!" + "\n"); this.reset(); } else //if they want to quit { text.appendText("Wow. What a skrub, okay bye." + "\n"); System.out.close(); } }); } } }); } } //end doBattle

即可完成此操作
not exists