q的符号真是混乱

[复制链接]
查看11 | 回复9 | 2012-5-21 10:19:41 | 显示全部楼层 |阅读模式
//q的符号真是混乱
定义表并初始化时,表名不用`
q)t

[]c1:`a`b`c`a;c2:20 15 10 20;c3:99.5 99.45 99.42 99.4)
q)
//查询也不用`
q)t
c1 c2 c3
-----------
a20 99.5
b15 99.45
c10 99.42
a20 99.4
//更新却需要`t
q)update c3:100.0 from `t where c1=`a
`t
q)t
c1 c2 c3
-----------
a20 100
b15 99.45
c10 99.42
a20 100
//不用`t只是显示更新后的结果
q)update c3:12.3 from t where c1=`a
c1 c2 c3
-----------
a20 12.3
b15 99.45
c10 99.42
a20 12.3
//实际数据没有变化
q)t
c1 c2 c3
-----------
a20 100
b15 99.45
c10 99.42
a20 100
回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
//书中讲到 Important: In order to modify the contents of texp you must refer to the table by name.
//Thus, after execution of the last query above, we still find,

回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
//第2个奇葩是delete 列名能删除表的某列
q)t

[]c1:`a`b`c;c2:`x`y`z)
q)t
c1 c2
-----
ax
by
cz
q)delete c2 from t
c1
--
a
b
c
q)t
c1 c2
-----
ax
by
cz
q)delete c2 from `t
`t
q)t
c1
--
a
b
c
回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
外键引用的表,不用在from中写,还是很强大
q)\l sp.q
+`p`city!(`p$`p1`p2`p3`p4`p5`p6`p1`p2;`london`london`london`london`london`lon..
(`s#+(,`color)!,`s#`blue`green`red)!+(,`qty)!,900 1000 1200
+`s`p`qty!(`s$`s1`s1`s1`s2`s3`s4;`p$`p1`p4`p6`p2`p2`p4;300 200 100 400 200 300)
q)s
s | namestatus city
--| -------------------
s1| smith 20 london
s2| jones 10 paris
s3| blake 30 paris
s4| clark 20 london
s5| adams 30 athens
q)p
p | namecolor weight city
--| -------------------------
p1| nut red 12 london
p2| boltgreen 17 paris
p3| screw blue17 rome
p4| screw red 14 london
p5| cam blue12 paris
p6| cog red 19 london
q)sp
spqty
---------
s1 p1 300
s1 p2 200
s1 p3 400
s1 p4 200
s4 p5 100
s1 p6 100
s2 p1 300
s2 p2 400
s3 p2 200
s4 p2 200
s4 p4 300
s1 p5 400
q)select qty by s from sp
s | qty
--| -----------------------
s1| 300 200 400 200 100 400
s2| 300 400
s3| ,200
s4| 100 200 300
q)select sname:s.name, qty by pname

.name from sp
pname| sname
qty
-----| ----------------------------------------
bolt | `smith`jones`blake`clark 200 400 200 200
cam| `clark`smith
100 400
cog| ,`smith
,100
nut| `smith`jones
300 300
screw| `smith`smith`clark 400 200 300
q)
回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
sp.q是软件自带的脚本
回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
〇〇 发表于 2016-5-7 10:59
//第2个奇葩是delete 列名能删除表的某列
q)t[]c1:`a`b`c;c2:`x`y`z)
q)t

//update也能增加列
Note: Unlike updates in SQL, update can add a new column.
t

[] c1:20 10 30 20; c2:`z`y`x`a)
t
c1 c2
-----
20 z
10 y
30 x
20 a
update c3:100+c1 from `t
`t
t
c1 c2 c3
---------
20 z120
10 y110
30 x130
20 a120

回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
〇〇 发表于 2016-5-8 18:54
//update也能增加列
Note: Unlike updates in SQL, update can add a new column.
t[] c1:20 ...

//不用计算列也可以增加列
q)update c4:5 6 7 8 from `t
`t
q)t
c1 c2 c3c4
------------
20 z120 5
10 y110 6
30 x130 7
20 a120 8
回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
〇〇 发表于 2016-5-8 18:57
//不用计算列也可以增加列
q)update c4:5 6 7 8 from `t
`t

//用update增加时行数必须一致,毕竟不是插入
q)update c5

til 5) from `t
'length
q)update c5

til 4) from `t
`t
q)t
c1 c2 c3c4 c5
---------------
20 z120 50
10 y110 61
30 x130 72
20 a120 83
回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
//建表初始化,0行和2行都可以,1行不行
q) t

[] name:`Dent`Beeblebrox; iq:98 42)
q)t
name iq
-------------
Dent 98
Beeblebrox 42
q) t

[] name:`Dent; iq:98)
'rank
q) t

[] name

); iq

))
q)t
name iq
-------
q)`t insert `Demt;98
'length
q)`t insert (`Dent;98)
,0
q)t
name iq
-------
Dent 98
回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
//插入支持从表和列表
q)`t insert select from t
,1
q)t
name iq
-------
Dent 98
Dent 98
q)`t insert select from t
2 3
q)t
name iq
-------
Dent 98
Dent 98
Dent 98
Dent 98
//from t,t 不会自连接笛卡儿积
q)`t insert select t.name,t.iq from t,t
4 5 6 7
q)t
name iq
-------
Dent 98
Dent 98
Dent 98
Dent 98
Dent 98
Dent 98
Dent 98
Dent 98
//第1列是字面量,第2列是生成的数字
q)`t insert (`a,`b,`c;til 3)
8 9 10
q)t
name iq
-------
Dent 98
Dent 98
Dent 98
Dent 98
Dent 98
Dent 98
Dent 98
Dent 98
a0
b1
c2
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行