CUBRID学习笔记 41 sql语法之select

cubrid的中sql查询语法

SELECT [ ]
[{TO | INTO} ][FROM ]
[WHERE ][GROUP BY {col_name | expr} [ASC | DESC], ...[WITH ROLLUP]]
[HAVING ]
[ORDER BY {col_name | expr} [ASC | DESC], ... [NULLS {FIRST | LAST}][LIMIT [offset,] row_count]
[USING INDEX { index_name [,index_name, ...] | NONE }][FOR UPDATE [OF ]]

<qualifier> ::= ALL | DISTINCT | DISTINCTROW | UNIQUE

<select_expressions> ::= * | <expression_comma_list> | *, <expression_comma_list>

<variable_comma_list> ::= [:] identifier, [:] identifier, ...

<extended_table_specification_comma_list> ::=
    <table_specification>   [
                                {, <table_specification> } ... |
                                <join_table_specification> ... |
                                <join_table_specification2> ...
                            ]

::=
[ ] [WITH ( )] |
[ ] |
|
TABLE ( )

::= [AS] [( )]

::= [ONLY] |
ALL [ EXCEPT ]

::= CLASS

::=
[INNER | {LEFT | RIGHT} [OUTER]] JOIN ON

::= CROSS JOIN

::= READ UNCOMMITTED

  • 还是看例子吧

distinct 去重
SELECT DISTINCT host_nation
FROM olympic;

分页
SELECT host_year as col1, host_nation as col2
FROM olympic
ORDER BY col2 LIMIT 5;

SELECT CONCAT(host_nation, ', ', host_city) AS host_place
FROM olympic
ORDER BY host_place LIMIT 5;

还可以
SELECT 1+1 AS sum_value;

其实就类似子查询

SELECT SUM (n)
FROM (SELECT gold FROM participant WHERE nation_code = 'KOR'
UNION ALL
SELECT silver FROM participant WHERE nation_code = 'JPN') AS t(n);

换个姿势
SELECT nation_code, host_year, gold
FROM participant p
WHERE gold > (SELECT AVG(s)
FROM (SELECT silver + bronze
FROM participant
WHERE nation_code = p.nation_code
AND silver > 0
AND bronze > 0)
AS t(s));

where条件

WHERE

<search_condition> ::=
    <comparison_predicate>
    <between_predicate>
    <exists_predicate>
    <in_predicate>
    <null_predicate>
    <like_predicate>
    <quantified_predicate>
    <set_predicate>
  
  比较运算
  
  = <> != > < >= <=
  
  成立是1 不成立是0 
  
  any  some  all运算
  
  any 任一个条件符合 some 是一部分(文档暂缺)
  
  SELECT * FROM condition_tbl WHERE dept_name = ANY{'devel','sales'};
  
  all  条件全部符合
  
  SELECT * FROM condition_tbl WHERE salary > ALL{3000000, 4000000, NULL};
  

  SELECT * FROM condition_tbl WHERE (

(0.9 * salary) < ALL (SELECT salary FROM condition_tbl
WHERE dept_name = 'devel')
);

between 运算符

SELECT * FROM condition_tbl WHERE salary BETWEEN 3000000 AND 4000000;

SELECT * FROM condition_tbl WHERE (salary >= 3000000) AND (salary <= 4000000);

SELECT * FROM condition_tbl WHERE salary NOT BETWEEN 3000000 AND 4000000;

SELECT * FROM condition_tbl WHERE name BETWEEN 'A' AND 'E';

exists 运算符

SELECT 'raise' FROM db_root WHERE EXISTS(
SELECT * FROM condition_tbl WHERE salary < 2500000);

SELECT 'raise' FROM db_root WHERE NOT EXISTS(
SELECT * FROM condition_tbl WHERE salary < 2500000);

in 运算符

SELECT * FROM condition_tbl WHERE dept_name IN {'devel','sales'};

SELECT * FROM condition_tbl WHERE dept_name = ANY{'devel','sales'};

SELECT * FROM condition_tbl WHERE dept_name NOT IN {'devel','sales'};

isnull

SELECT * FROM condition_tbl WHERE salary IS NULL;

SELECT * FROM condition_tbl WHERE salary IS NOT NULL;

SELECT * FROM condition_tbl WHERE salary = NULL;

like 运算符
转义用\
SELECT * FROM condition_tbl WHERE name LIKE '%s%';

SELECT * FROM condition_tbl WHERE UPPER(name) LIKE '_O%';

SELECT * FROM condition_tbl WHERE name LIKE '___';

REGEXP, RLIKE 运算符
这个例子用的少,转码比较
SELECT ('a' collate utf8_en_ci REGEXP BINARY 'A' collate utf8_en_ci);
0
SELECT ('a' collate utf8_en_cs REGEXP BINARY 'A' collate utf8_en_cs);
0
SELECT ('a' COLLATE iso88591_bin REGEXP 'A' COLLATE iso88591_bin);
1
SELECT ('a' COLLATE iso88591_bin REGEXP BINARY 'A' COLLATE iso88591_bin);
0
主要看这个
支持正则表达式喽
SELECT name FROM athlete where name REGEXP '^[a-d]';

下面更像是演示正则表达式
匹配特殊的字符
SELECT ('new\nline' REGEXP 'new
line');

SELECT ('cubrid dbms' REGEXP '^cub');
SELECT ('this is cubrid dbms' REGEXP 'dbms$');

SELECT ('cubrid dbms' REGEXP '^c.*$');
SELECT ('Aaaapricot' REGEXP '^A+pricot');
SELECT ('Apricot' REGEXP '^Aa?pricot');
SELECT ('Aapricot' REGEXP '^Aa?pricot');
('Aapricot' regexp '^Aa?pricot')

1 代表成立
SELECT ('Aaapricot' REGEXP '^Aa?pricot');

('Aaapricot' regexp '^Aa?pricot')

0
-- (cub)* : match zero or more instances of the sequence abc.

SELECT ('cubcub' REGEXP '^(cub)*$');

('cubcub' regexp '^(cub)*$')

1
-- [a-dX], [^a-dX] : matches any character that is (or is not, if ^ is used) either a, b, c, d or X.

SELECT ('aXbc' REGEXP '^[a-dXYZ]+');

('aXbc' regexp '^[a-dXYZ]+')

1
SELECT ('strike' REGEXP '^[^a-dXYZ]+$');

('strike' regexp '^[^a-dXYZ]+$')

1

case 运算符

SELECT a,
CASE WHEN a=1 THEN 'one'
WHEN a=2 THEN 'two'
ELSE 'other'
END
FROM case_tbl;

结果
a case when a=1 then 'one' when a=2 then 'two' else 'other' end

        1  'one'
        2  'two'
        3  'other'
     NULL  'other'
     
     
     SELECT a,
   CASE a WHEN 1 THEN 'one'
          WHEN 2 THEN 'two'
          ELSE 'other'
   END

FROM case_tbl;

SELECT a,
CASE WHEN a=1 THEN 1
WHEN a=2 THEN 1.2345
ELSE 1.234567890
END
FROM case_tbl;

SELECT a,
CASE WHEN a=1 THEN 'one'
WHEN a=2 THEN 'two'
ELSE 1.2345
END
FROM case_tbl; //报错,类型转换错误

COALESCE 函数
对null处理的函数
类似
CASE WHEN a IS NOT NULL
THEN a
ELSE b
END

当a的值为null的时候 返回10.0000
SELECT a, COALESCE(a, 10.0000) FROM case_tbl;

decode 函数
类似
CASE WHEN a = b THEN c
WHEN a = d THEN e
ELSE f
END

参数有三个,
第一个 可以是表达式要或者比较的值 如下例的a
第二个 结果 如下例的1 ,2
第三个 默认值 如other
a 列
1
2
3
NULL

执行
SELECT a, DECODE(a, 1, 'one', 2, 'two', 'other') FROM case_tbl;
结果
1 'one'
2 'two'
3 'other'
NULL 'other'

a为1 等于one a为2 等于two a为3和null ,没有符合条件的项,使用默认值other

SELECT a, DECODE(a, 1, 1, 2, 1.2345, 1.234567890) FROM case_tbl;
注意以下的结果,数字都是单精度的.隐式转换了. 以默认结果的类型为准.
1 1.000000000
2 1.234500000
3 1.234567890
NULL 1.234567890
下面的这个会失败,因为one不能转为 double
SELECT a, DECODE(a, 1, 'one', 2, 'two', 1.2345) FROM case_tbl;

GREATEST 函数
获取最大的值

SELECT gold, silver , bronze, GREATEST (gold, silver, bronze)
FROM participant
WHERE nation_code = 'KOR';

结果是三个参数列每行值中的的最大值
gold silver bronze greatest(gold, silver, bronze)

        9           12            9                              12
        8           10           10                              10
        7           15            5                              15
       12            5           12                              12
       12           10           11                              12
       
     c#,net,cubrid,教程,学习,笔记欢迎转载 ,转载时请保留作者信息。本文版权归本人所有,如有任何问题,请与我联系[email protected] 。 过错  

if函数
三个参数,
第一个表达式返回真假
第二个表示真的时候返回的值
第三个表示假的时候返回的值

类似
CASE WHEN a IS TRUE THEN b
ELSE c
END

SELECT a, IF(a=1, 'one', 'other') FROM case_tbl;

IFNULL NVL函数
给null的列设默认值,注意返回的类型,取决于默认值的类型.ifnull和Nvl类似.但是Nvl支持集合类型.
类似
CASE WHEN a IS NULL THEN b
ELSE a
END

例如SELECT a, IFNULL(a, 'UNKNOWN') FROM case_tbl;
结果
1 '1'
2 '2'
3 '3'
NULL 'UNKNOWN'

ISNULL函数 返回整数 1或者0

LEAST函数
参数可以是多个表达式,返回最小的值, 参数是null则返回null

SELECT gold, silver , bronze, LEAST(gold, silver, bronze) FROM participant
WHERE nation_code = 'KOR';
结果
gold silver bronze least(gold, silver, bronze)

        9           12            9                            9
        8           10           10                            8
        7           15            5                            5
       12            5           12                            5
       12           10           11                           10

NULLIF 函数
相等返回null 否则原样返回
类似CASE
WHEN a = b THEN NULL
ELSE a
END

      SELECT a, NULLIF(a, 1) FROM case_tbl; 
      结果 a是1的时候返回null
       a  nullif(a, 1)

        1          NULL
        2             2
        3             3
     NULL          NULL
     
     

NVL2(expr1, expr2, expr3) 函数
第一个表达式不等于null的是否返回第二个表达式,等于null返回第三个表达式
SELECT a, NVL2(a, a+1, 10.5678) FROM case_tbl;

     a  nvl2(a, a+1, 10.5678)

        1                      2
        2                      3
        3                      4
     NULL                     11

你可能感兴趣的:(CUBRID学习笔记 41 sql语法之select)