From 4423e78a6266719aa8446993a77fceb4beb5b56d Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Mon, 1 Jan 2024 23:08:40 -0500 Subject: [PATCH] vault backup: 2024-01-01 23:08:40 --- Databases/SurrealDB/Syntax.md | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Databases/SurrealDB/Syntax.md b/Databases/SurrealDB/Syntax.md index 64210b2..c3f456f 100644 --- a/Databases/SurrealDB/Syntax.md +++ b/Databases/SurrealDB/Syntax.md @@ -31,4 +31,47 @@ SELECT name AS user_name, address FROM person; SELECT * FROM ONLY person:john; ``` +### Advanced `SELECT` expressions + +```SQL +-- Select nested objects/values +SELECT address.city FROM person; + +-- Select all nested array values +-- note the .* syntax works to select everything from an array or object-like values +SELECT address.*.coordinates AS coordinates FROM person; +-- Equivalent to +SELECT address.coordinates AS coordinates FROM person; + +-- Select one item from an array +SELECT address.coordinates[0] AS latitude FROM person; + +-- Select unique values from an array +SELECT array::distinct(tags) FROM article; + +-- Select unique values from a nested array across an entire table +SELECT array::group(tags) AS tags FROM article GROUP ALL; + +-- Use mathematical calculations in a select expression +SELECT ( ( celsius * 2 ) + 30 ) AS fahrenheit FROM temperature; + +-- Return boolean expressions with an alias +SELECT rating >= 4 as positive FROM review; + +-- Select manually generated object structure +SELECT { weekly: false, monthly: true } AS `marketing settings` FROM user; + +-- Select filtered nested array values +SELECT address[WHERE active = true] FROM person; + +-- Select a person who has reacted to a post using a celebration +-- You can see the graph as: person->(reacted_to WHERE type='celebrate')->post +SELECT * FROM person WHERE ->(reacted_to WHERE type='celebrate')->post; + +-- Select a remote field from connected out graph edges +SELECT ->likes->friend.name AS friends FROM person:tobie; + +-- Use the result of a subquery as a returned field +SELECT *, (SELECT * FROM events WHERE type = 'activity' LIMIT 5) AS history FROM user; +``` ### \ No newline at end of file