思源笔记社区正在搭建中,现邀您共建
SiYuan Community is under construction. Join us to co-build.
原贴地址:https://ld246.com/article/1683355095671
作者:F 酱


select 查询,如果你去翻阅一个正规 SQL 教程,他们会告诉你 select 语法的定义如下。<condition> 在这里指代查询的条件。如果要查询所有列,则可以用 * 代替列名。SELECT column1, column2, ...
FROM table_name
where <condition>;<condition> 改成**==自己需要的条件==**就行了。<condition> 来匹配到我们希望的块<condition>
<condition> 是什么鬼?它在 SQL 里面被称为「查询条件」。具体语法我们一步一步来type 我们把它称为 key'd' 我们称为 value= 被称为比较操作符,还有其他很多别的操作符:= 代表了相等< 代表了小于> 代表了大于!= 代表了不相等like 代表了字符串模板匹配
created ,表示一个块是什么时候创建的,他的值的格式为 20210104091228,我们依样画葫芦,「2023 年 4 月之后」就是应该写作:「created > '20230401000000'」。and 语句: <cond1> and <cond2>,最后的 sql 查询写成这样:
cond1 or cond2 然后和 cond3 求交集呢?还是 cond1 和 cond2 and cond3 求并集呢?LIKELIKE,这个符号我们要额外讲一下。思源的数据库项目的数据类型基本都是字符串,因而字符串的比较是很重要的。我们在前面提到过的 = 代表的含义是「完全相等」。比如我们前面提到的:LIKE 的大显身手之处了。它用于匹配一种模式,用法和 = 类似也是:%:匹配零个或多个字符。_:匹配单个字符。% 就可以把这些无关紧要的信息全部省略,可以说很方便了。
def_block_id 和 block_id 两个字段来表示的。这两个字段分别指向了被引用的块和引用该块的块的 id。这样,我们可以通过 refs 表来查询两个块之间的关系。


union 查询:用于将多个查询结果合并为一个结果集。order by:用于对查询结果按照指定的列进行排序。limit:用于限制查询结果的数量。group by:用于将查询结果按照指定的列进行分组。having:用于在 group by 后筛选分组结果。join:用于将多个表的数据连接起来查询。
select * from blocks

select * from blocks 这个语句不用管直接无脑套用就行了吗?原因在于嵌入块的查询最终的结果是要渲染成可以编辑的块的,所以:from blocks,其他的表查询了也渲染不了select *,如果字段缺失,嵌入块没法渲染一个完整的块

data/templates 目录下创建若干 md 文件,这些 md 文件就可以作为模板导入思源。(当然为了方便管理,还是建议大家先创建一个文件夹,把自己的模板放到这个文件夹里面)。{{SELECT * from blocks_esc_newline_WHERE `type` = "l" AND `subtype` = "t"_esc_newline_AND `created` > strftime('%Y%m%d%H%M%S', datetime('now', '-7 day')) _esc_newline_AND markdown REGEXP "\* \[ \] \S+"_esc_newline_AND `parent_id` not in (_esc_newline_ select `id` from blocks where `subtype` = "t"_esc_newline_)_esc_newline_ORDER BY `created` DESC}}
/ 导入模板,立刻就可以把这个嵌入块快速导入进来:
```sql
select * from blocks where type = 'd' and created > '20230501000000';
```
{: custom-type="query-code"}
<iframe src="/widgets/Query" data-src="/widgets/Query" data-subtype="widget"></iframe>
{: custom-query-fields="["type", "content", "hpath", "memo", "box"]" custom-type="query-widget" custom-query-limit=""len"" custom-query-maxrow="2" custom-query-maxlen="20"}
{: } 语法了吗,它可以为近邻的 markdown 元素定义一些额外的属性,而思源会把这些属性作为块的属性一起导入。,举个例子,上面提到的 sql 代码块:```sql
select * from blocks where type = 'd' and created > '20230501000000';
```
{: custom-type="query-code"}custom- 开头的属性会被当作用户定义的自定义属性)
```sql
select * from blocks where id in (
select block_id from refs where def_block_id = '这里填写文档 ID'
) order by `updated` desc
```
{: custom-type="query-code"}
<iframe src="/widgets/Query" data-src="/widgets/Query" data-subtype="widget" border="0" frameborder="no" framespacing="0" allowfullscreen="true" style="width: 374px; height: 37px; border: none; background-color: transparent;"></iframe>
{: custom-query-fields="["type", "content", "hpath", "memo", "box"]" custom-type="query-widget" custom-query-limit=""len"" custom-query-maxrow="2" custom-query-maxlen="20"}.action{.id}:它会被渲染为模板所在文档的 id.action{.title}:它会被渲染为模板所在文档的标题```sql
select * from blocks where id in (
select block_id from refs where def_block_id = '.action{.id}'
) order by `updated` desc
```
{: custom-type="query-code"}
<iframe src="/widgets/Query" data-src="/widgets/Query" data-subtype="widget" border="0" frameborder="no" framespacing="0" allowfullscreen="true" style="width: 374px; height: 37px; border: none; background-color: transparent;"></iframe>
{: custom-query-fields="["type", "content", "hpath", "memo", "box"]" custom-type="query-widget" custom-query-limit=""len"" custom-query-maxrow="2" custom-query-maxlen="20"}| 字段名 | 说明 | 字段值示例 |
|---|---|---|
id | 内容块 ID | 20210104091228-d0rzbmm |
parent_id | 双亲块 ID,如果内容块 是文档块则该字段为空 | 20200825162036-4dx365o |
root_id | 文档块 ID | 20200825162036-4dx365o |
hash | content 字段的 SHA256 校验和 | a75d25c |
box | 笔记本 ID | 20210808180117-czj9bvb |
path | 内容块所在文档路径 | /20200812220555-lj3enxa/20210808180320-abz7w6k/20200825162036-4dx365o.sy |
hpath | 人类可读的内容块所在文档路径 | /0 请从这里开始/编辑器/排版元素 |
name | 内容块名称 | 一级标题命名 |
alias | 内容块别名 | 一级标题别名 |
memo | 内容块备注 | 一级标题备注 |
tag | 非文档块为块内包含的标签,文档块为文档的标签 | #标签1 #标签2# #标签3# |
content | 去除了 Markdown 标记符的文本 | 一级标题 |
fcontent | 存储容器块第一个子块的内容(1.9.9 添加) | 第一个子块 |
markdown | 包含完整 Markdown 标记符的文本 | # 一级标题 |
length | markdown 字段文本 长度 | 6 |
type | 内容块类型,参考⸢类型字段⸥ | h |
subtype | 内容块子类型,参考⸢子类型字段⸥ | h1 |
ial | 内联属性列表,形如 {: name="value"} | {: id="20210104091228-d0rzbmm" updated="20210604222535"} |
sort | 排序权重,数值越小排序越靠前 | 5 |
created | 创建时间 | 20210104091228 |
updated | 更新时间 | 20210604222535 |
| 字段名 | 说明 | 字段值示例 |
|---|---|---|
id | 引用 ID | 20211127144458-idb32wk |
def_block_id | 被引用块的块 ID | 20200925095848-aon4lem |
def_block_parent_id | 被引用块的双亲节点的块 ID | 20200905090211-2vixtlf |
def_block_root_id | 被引用块所在文档的 ID | 20200905090211-2vixtlf |
def_block_path | 被引用块所在文档的路径 | /20200812220555-lj3enxa/20210808180320-fqgskfj/20200905090211-2vixtlf.sy |
block_id | 引用所在内容块 ID | 20210104090624-c5bu25o |
root_id | 引用所在文档块 ID | 20200905090211-2vixtlf |
box | 引用所在笔记本 ID | 20210808180117-czj9bvb |
path | 引用所在文档块路径 | /20200812220555-lj3enxa/20210808180320-fqgskfj/20200905090211-2vixtlf.sy |
content | 引用锚文本 | 元类型 |
markdown | 包含完整 Markdown 标记符的文本 | ((20200925095848-aon4lem "元类型")) |
type | 引用类型 | ref_id |
| 字段名 | 说明 | 字段值示例 字段值示例 |
|---|---|---|
id | 属性 ID 属性 ID | 20211127144458-h7y55zu |
name | 属性名称 属性名称 | bookmark |
value | 属性值 | ✨ |
type | 类型 | b |
block_id | 块 ID 块 ID | 20210428212840-859h45j |
root_id | 文档 ID 文档 ID | 20200812220555-lj3enxa |
box | 笔记本 ID 笔记本 ID | 20210808180117-czj9bvb |
path | 文档文件路径 文档文件路径 | /20200812220555-lj3enxa.sy |