MySQL Classic SQL collection

Source: Internet
Author: User

1. SQL data storage by condition

普通的 INSERT INTO 插入:
INSERT INTO card(cardno, cardnum) VALUES(‘1111‘, ‘100‘);INSERT INTO card(cardno, cardnum) VALUES(‘2222‘, ‘200‘);

Inserting a condition into SQL

insert  INTO  Card (Cardno, Cardnum) select   ' 111 ' ,  ' " from  DUAL where  not  exists  (select  cardno from  Card where  Cardno =  ' 111 ' );  INSERT  into card (Cardno, cardnum) SELECT ' 222 ', '  + ' from DUAL where  not EXISTS(SELECT cardno  from card WHERE Cardno = ' 222 ');

2. Condition control

IF 语句CASE 语句IF 语句根据条件执行一系列语句,有三种形式:IF-THEN、IF-THEN-ELSE 和 IF-THEN-ELSIF
DECLARE  x VARCHAR2(10);BEGIN  x:=‘&x‘;  IF LOWER(x)=‘yTHEN    DBMS_OUTPUT.PUT_LINE(‘Yes!‘);  ELSIF LOWER(x)=‘nTHEN    DBMS_OUTPUT.PUT_LINE(‘No!‘);  ELSE    DBMS_OUTPUT.PUT_LINE(‘Error‘);  ENDIF;END;

The case statement is used to compare multiple values against a single variable or an expression
Calculates the value of a selector before executing a case statement

begin   Case  ' &grade ' when  ' A ' then  dbms_ OUTPUT.      Put_Line (' excellent '); when  ' B ' then  dbms_output.      Put_Line (excellent '); when  ' C ' then  dbms_output.      Put_Line (' good '); when  ' D ' then  dbms_output.      Put_Line (' General '); when  ' F ' then  dbms_output.      Put_Line (' poor '); else  Dbms_output.    Put_Line (' No such result '); end  case ; end ;  

3. Loop control for repeating a series of statements
Loop control statements include:
LOOP, exit, and exit when
Three types of loop control:
Loop-Unconditional Loop
While-looping by condition
For-loop fixed number of times

LOOP   sequence_of_statementsENDLOOP;WHILELOOP   sequence_of_statementsENDLOOP;FORIN [REVERSE] value1..value2LOOP   sequence_of_statementsENDLOOP;

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MySQL Classic SQL collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.