Parameterized queries

This post was last updated for 341 days ago, and the information may already be changed

Hello,

I would like to be able to use an attribute of the parent block when writing an SQL query in an embed block. For the sake of example, consider I am in the document Trip to the Moon​ and I want to make a query that collects links to it. I would write

select b.* from blocks as b where b.content like "%trip to the moon%" order by updated desc

But I am already in Trip to the Moon​ (a document or a parent block with that name), so ideally I would like to grab the current document's title/name. I would like to replace the literal string "%trip to the moon%"​ with something like :parent.title​. How can I achieve this?

    Welcome to here!

    Here we can learn from each other how to use SiYuan, give feedback and suggestions, and build SiYuan together.

    Signup About
    Please input reply content ...
    • 88250

      We are very sorry. Due to some security reasons, LianDi cannot be accessed without logging in.

    • MiscReply
    • ciwoyipang

      In addition, the embedding blocks of the ontology support JS.You can use plugins to enhance embedded block queries.

      siyuan-plugin-data-query

      image.png

    • alvorithm
      VIP Warrior Author

      Hi,

      Tagging @zuoqiu as well (author of widget-query?).

      Reading the documentation of the widget and exploring the Refs table (use the source 😉 ), I have come up with the following SQL query

      SELECT src.* FROM blocks AS src WHERE src.id in
      	(SELECT refs.block_id from refs 
      		WHERE refs.def_block_id='.block{.root_id}');
      

      This actually does what I want, i.e. it lists all the blocks pointing to the query's root block (in this case, the doc that contains the query). But

      1. widget-query renders the output as a table. I would like to render the blocks normally. Is there a way to do it like in embed block queries?
      2. alternatively, I could in principle use the same query in an embed block. But when I do, this produces no results. My question here: is the dot syntax .block{.root_id} that accesses properties of the current block not allowed in normal embeds @88250 ?

      I have searched extensively but so far I haven't found a comprehensive documentation of the query system, though I am thankful to @leolee9086 for sharing some examples in the forum.

      Please let me know if you find ways to improve my query above!

      Thank you everybody for your patience, and apologies that I just discovered the forum has a threaded reply function.

      Á.

      edit:

      1. changed = to IN in query so as to be able to list multiple incoming links.
      2. simplified the nested query, blocks as def not needed for it
      1 Reply
      2 Operate
      alvorithm updated this reply at 2023-12-21 01:10:59
      alvorithm updated this reply at 2023-12-21 01:06:30
    • alvorithm
      VIP Warrior Author

      Dear @ciwoyipang, thank you so much for making me aware of the query widget. It is a very featureful extension. But I think I did not describe my need clearly. What I am after is that the query block asks its parent for the text "trip to the moon", i.e. that string in the query should be generated automatically from the context.

      select * from blocks as b where b.content like "%insert here the title or the name of the parent block%"

      So if I put this query inside a document named "The mother of all queries" it would automatically fill in the value and execute

      select * from blocks as b where b.content like "%The mother of all queries%"

      Do you have an idea on how to customize queries based on the context of the query block (e.g. the parent, or the preceding block)?

      image.png

      PS. in order to have the type column values translated, is it necessary to do it in Siyuan or in the widget code?

    • Visit all replies