分享
 
 
 

Sybase及SQLAnywhereSQL语句小结

王朝mssql·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

根据SQL Anywhere User's Guide所作小结。绝大部分都可用直接于Sybase数据库。

SELECT语句

SELECT *

FROM employee

SELECT *

FROM employee

ORDER BY emp_lname ASC

SELECT *

FROM employee

ORDER BY emp_lname DESC

SELECT emp_lname, dept_id, birth_date

FROM employee

SELECT *

FROM employee

WHERE emp_fname='John'

(一定使用单引号)

SELECT emp_fname, emp_lname, birth_date

FROM employee

WHERE emp_fname = 'John'

ORDER BY birth_date

SELECT emp_lname, birth_date

FROM employee

WHERE birth_date < 'March 3, 1964'

(=、<、>、<=、>=、<>,加上AND与OR)

SELECT emp_lname, emp_fname

FROM employee

WHERE emp_lname LIKE 'br%'

(%、_)

SELECT emp_lname, emp_fname

FROM employee

WHERE SOUNDEX( emp_lname ) = SOUNDEX( 'Brown' )

(找出英文中发音相同的记录,中文下用处不大)

SELECT emp_lname, birth_date

FROM employee

WHERE birth_date BETWEEN '1965-1-1' AND '1965-3-31'

SELECT emp_lname, emp_id

FROM employee

WHERE emp_lname IN ('yeung', 'bucceri', 'charlton')

连接表

SELECT *

FROM sales_order, employee

WHERE sales_order.sales_rep = employee.emp_id

SELECT E.emp_lname, S.id, S.order_date

FROM sales_order as S, employee as E

WHERE S.sales_rep = E.emp_id

ORDER BY E.emp_lname

连接两表的快捷键:KEY JOIN及NATURAL JOIN,最好用WHERE.

SELECT emp_lname, id, order_date

FROM sales_order

KEY JOIN employee

(主键与外部键对应的地方,就可以用KEY JOIN)

SELECT company_name,

CAST( SUM(sales_order_items.quantity * product.unit_price) AS INTEGER) AS value

FROM customer

KEY JOIN sales_order

KEY JOIN sales_order_items

KEY JOIN product

GROUP BY company_name

SELECT emp_lname, dept_name

FROM employee

NATURAL JOIN department

(找出两表间有相同的字段名,进行连结)

集合

SELECT count( * )

FROM employee

SELECT count( * ),

min( birth_date ),

max( birth_date )

FROM employee

(MIN, MAX, COUNT, AVG, SUM, LIST,作为单独的一列选出)

SELECT sales_rep, count( * )

FROM sales_order

GROUP BY sales_rep

(在使用GROUP BY时,对于GROUP BY指定的字段,其每一个不同的值都会组成一行)

SELECT sales_rep, count( * )

FROM sales_order

KEY JOIN employee

GROUP BY sales_rep

HAVING count( * ) > 55

更新数据库

INSERT

INTO department ( dept_id, dept_name, dept_head_id )

VALUES ( 220, 'Eastern Sales', 902 )

INSERT

INTO department

VALUES ( 220, 'Eastern Sales', 902 )

UPDATE employee

SET dept_id = 400, manager_id = 1576

WHERE emp_id = 195

DELETE

FROM employee

WHERE termination_date IS NOT NULL

DELETE

FROM employee

WHERE LEFT( phone, 3 ) = '617' AND manager_id = 902

视图

CREATE VIEW emp_dept AS

SELECT emp_fname, emp_lname, dept_name

FROM employee

JOIN department ON department.dept_id = employee.dept_id

SELECT *

FROM emp_dept

(视图能自动更新状态)

DROP VIEW emp_dept

CREATE VIEW emp_dept(FirstName, LastName, Department) AS

SELECT emp_fname, emp_lname, dept_name

FROM employee JOIN department ON department.dept_id = employee.dept_id

(创建视图不能使用ORDEY BY,但使用视图可以使用)

SELECT LastName, dept_head_id

FROM emp_dept, department

WHERE emp_dept.Department = department.dept_name

(将视图与其他表进行进一步的连结)

视图权限管理

GRANT CONNECT TO M_Kelly

IDENTIFIED BY SalesHead

CREATE VIEW SalesEmployee AS

SELECT emp_id, emp_lname, emp_fname

FROM "dba".employee

WHERE dept_id = 200

GRANT SELECT ON SalesEmployee TO M_Kelly

CONNECT USER M_Kelly IDENTIFIED BY SalesHead ;

SELECT * FROM "dba".SalesEmployee

子查询

SELECT *

FROM sales_order_items

WHERE prod_id IN

( SELECT id

FROM product

WHERE quantity < 20 )

ORDER BY ship_date DESC

SELECT *

FROM fin_data

WHERE fin_data.code = ANY ( SELECT fin_code.code

FROM fin_code

WHERE type = 'revenue' )

(=ANY 相当于IN)

SELECT *

FROM fin_data

WHERE fin_data.code <> ALL ( SELECT fin_code.code

FROM fin_code

WHERE type = 'revenue' )

(相当于NOT IN)

SELECT sales_order.id, sales_order.order_date,

( SELECT company_name

FROM customer

WHERE customer.id = sales_order.cust_id )

FROM sales_order

WHERE order_date > '1994/01/01'

ORDER BY order_date

(如果其他表只要求产生一个字段,就可以使用子查询来代替连接)

SELECT company_name, state,

( SELECT MAX( id )

FROM sales_order

WHERE sales_order.cust_id = customer.id )

FROM customer

WHERE state = 'WA'

根据SQL Anywhere User's Guide所作小结。绝大部分都可用直接于Sybase数据库。

SELECT语句

SELECT *

FROM employee

SELECT *

FROM employee

ORDER BY emp_lname ASC

SELECT *

FROM employee

ORDER BY emp_lname DESC

SELECT emp_lname, dept_id, birth_date

FROM employee

SELECT *

FROM employee

WHERE emp_fname='John'

(一定使用单引号)

SELECT emp_fname, emp_lname, birth_date

FROM employee

WHERE emp_fname = 'John'

ORDER BY birth_date

SELECT emp_lname, birth_date

FROM employee

WHERE birth_date < 'March 3, 1964'

(=、<、>、<=、>=、<>,加上AND与OR)

SELECT emp_lname, emp_fname

FROM employee

WHERE emp_lname LIKE 'br%'

(%、_)

SELECT emp_lname, emp_fname

FROM employee

WHERE SOUNDEX( emp_lname ) = SOUNDEX( 'Brown' )

(找出英文中发音相同的记录,中文下用处不大)

SELECT emp_lname, birth_date

FROM employee

WHERE birth_date BETWEEN '1965-1-1' AND '1965-3-31'

SELECT emp_lname, emp_id

FROM employee

WHERE emp_lname IN ('yeung', 'bucceri', 'charlton')

连接表

SELECT *

FROM sales_order, employee

WHERE sales_order.sales_rep = employee.emp_id

SELECT E.emp_lname, S.id, S.order_date

FROM sales_order as S, employee as E

WHERE S.sales_rep = E.emp_id

ORDER BY E.emp_lname

连接两表的快捷键:KEY JOIN及NATURAL JOIN,最好用WHERE.

SELECT emp_lname, id, order_date

FROM sales_order

KEY JOIN employee

(主键与外部键对应的地方,就可以用KEY JOIN)

SELECT company_name,

CAST( SUM(sales_order_items.quantity * product.unit_price) AS INTEGER) AS value

FROM customer

KEY JOIN sales_order

KEY JOIN sales_order_items

KEY JOIN product

GROUP BY company_name

SELECT emp_lname, dept_name

FROM employee

NATURAL JOIN department

(找出两表间有相同的字段名,进行连结)

集合

SELECT count( * )

FROM employee

SELECT count( * ),

min( birth_date ),

max( birth_date )

FROM employee

(MIN, MAX, COUNT, AVG, SUM, LIST,作为单独的一列选出)

SELECT sales_rep, count( * )

FROM sales_order

GROUP BY sales_rep

(在使用GROUP BY时,对于GROUP BY指定的字段,其每一个不同的值都会组成一行)

SELECT sales_rep, count( * )

FROM sales_order

KEY JOIN employee

GROUP BY sales_rep

HAVING count( * ) > 55

更新数据库

INSERT

INTO department ( dept_id, dept_name, dept_head_id )

VALUES ( 220, 'Eastern Sales', 902 )

INSERT

INTO department

VALUES ( 220, 'Eastern Sales', 902 )

UPDATE employee

SET dept_id = 400, manager_id = 1576

WHERE emp_id = 195

DELETE

FROM employee

WHERE termination_date IS NOT NULL

DELETE

FROM employee

WHERE LEFT( phone, 3 ) = '617' AND manager_id = 902

视图

CREATE VIEW emp_dept AS

SELECT emp_fname, emp_lname, dept_name

FROM employee

JOIN department ON department.dept_id = employee.dept_id

SELECT *

FROM emp_dept

(视图能自动更新状态)

DROP VIEW emp_dept

CREATE VIEW emp_dept(FirstName, LastName, Department) AS

SELECT emp_fname, emp_lname, dept_name

FROM employee JOIN department ON department.dept_id = employee.dept_id

(创建视图不能使用ORDEY BY,但使用视图可以使用)

SELECT LastName, dept_head_id

FROM emp_dept, department

WHERE emp_dept.Department = department.dept_name

(将视图与其他表进行进一步的连结)

视图权限管理

GRANT CONNECT TO M_Kelly

IDENTIFIED BY SalesHead

CREATE VIEW SalesEmployee AS

SELECT emp_id, emp_lname, emp_fname

FROM "dba".employee

WHERE dept_id = 200

GRANT SELECT ON SalesEmployee TO M_Kelly

CONNECT USER M_Kelly IDENTIFIED BY SalesHead ;

SELECT * FROM "dba".SalesEmployee

子查询

SELECT *

FROM sales_order_items

WHERE prod_id IN

( SELECT id

FROM product

WHERE quantity < 20 )

ORDER BY ship_date DESC

SELECT *

FROM fin_data

WHERE fin_data.code = ANY ( SELECT fin_code.code

FROM fin_code

WHERE type = 'revenue' )

(=ANY 相当于IN)

SELECT *

FROM fin_data

WHERE fin_data.code <> ALL ( SELECT fin_code.code

FROM fin_code

WHERE type = 'revenue' )

(相当于NOT IN)

SELECT sales_order.id, sales_order.order_date,

( SELECT company_name

FROM customer

WHERE customer.id = sales_order.cust_id )

FROM sales_order

WHERE order_date > '1994/01/01'

ORDER BY order_date

(如果其他表只要求产生一个字段,就可以使用子查询来代替连接)

SELECT company_name, state,

( SELECT MAX( id )

FROM sales_order

WHERE sales_order.cust_id = customer.id )

FROM customer

WHERE state = 'WA'

SELECT company_name, MAX( sales_order.id ),state

FROM customer

KEY LEFT OUTER JOIN sales_order

WHERE state = 'WA'

GROUP BY company_name, state

系统表

SYSCATALOG,查看所有的表

SYSCOLUMNS, 查看表的字段属性

FROM customer

KEY LEFT OUTER JOIN sales_order

WHERE state = 'WA'

GROUP BY company_name, state

系统表

SYSCATALOG,查看所有的表

SYSCOLUMNS, 查看表的字段属性

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有