I have a MySQL DB containing a table named "products".
This table contains a Json Data_type column named "values".
I would like to find the path to extract a specific value :
select JSON_EXTRACT(values, '$.COD')
from products
where id = '1'
returns :
"COD": {"<channels>": {"<locales>": "3699999999999"}}
And what I want is "3699999999999".
It is pretty obvious that my path is not the good one, but I can't find the solution.
Thanks for your help !
You can try
SELECT JSON_EXTRACT(`values`, '$.COD.<channels>.<locales>') FROM products WHERE id = '1';
For more details please refer mysql-for-your-json
Related
I have a query where i have "TEST"."TABLE" LEFT JOINED to PUBLIC."SchemaKey". Now in my final select statement i have a case statement where i check if c."Type" = 'FOREIGN' then i want to grab a value from another table but the table name value i am using in that select statement is coming from the left joined table column value. I've tried multiple ways to get to work but i keep getting an error, although if i hard code the table name it seems to work. i need the table name to come from c."FullParentTableName". Is what i am trying to achieve possible in snowflake and is there a way to make this work ? any help would be appreciated !
SELECT
c."ParentColumn",
c."FullParentTableName",
a."new_value",
a."column_name"
CASE WHEN c."Type" = 'FOREIGN' THEN (SELECT "Name" FROM TABLE(c."FullParentTableName") WHERE "Id" = 'SOME_ID') ELSE null END "TestColumn" -- Need assistance on this line...
FROM "TEST"."TABLE" a
LEFT JOIN (
select s."Type", s."ParentSchema", s."ParentTable", s."ParentColumn", concat(s."ParentSchema",'.','"',s."ParentTable",'"') "FullParentTableName",s."ChildSchema", s."ChildTable", trim(s."ChildColumn",'"') "ChildColumn"
from PUBLIC."SchemaKey" as s
where s."Type" = 'FOREIGN'
and s."ChildTable" = 'SOMETABLENAME'
and "ChildSchema" = 'SOMESCHEMANAME'
) c
on a."column_name" = c."ChildColumn"
Thanks !
In Snowflake you cannot dynamically use the partial results as tables.
You can use a single bound value via identifier to bind a value to table name
But you could write a Snowflake Scripting but it would need to explicitly join the N tables. Thus if you N is fixed, you should just join those.
I have an sql query like the following
select *
from register_bs
INNER JOIN spouse_details ON register_bs.reg = spouse_details.reg
WHERE country NOT IN('Australia', 'USA', 'Germany', 'Canada');
This query is displaying correct values for all fields except the 'id' field, it's giving some random id for every data. for example the below data:
when i click on any button of the above data it is going to edit page with different data like below
because the id got is wrong
As there are 900 columns in my register_bs table, I cant use field alias instead of *. Can anyone please tell me how to correct my statement to get the correct id? Thanks in advance
First execute the below query in server and check the values of ID
SELECT register_bs.ID AS ValidateedID, register_bs.*, spouse_details.*
FROM register_bs INNER JOIN spouse_details ON register_bs.reg = spouse_details.reg
WHERE country NOT IN('Australia', 'USA', 'Germany', 'Canada');
Kindly check if the two tables that you have joined have the same column name for their id.
If yes, specify the table first and then the column of the id you want to retrieve.
e.g. register_bs.id
I'm trying to write a query that would append my JSON variable("data") to a JSON field in my database. I have a table called cart with three fields inside: id type int, status type varchar and items type json. So basically I'm trying to write a query where it would find my cart by the id and it would add an item to the end of my item field so far what I have is this:
query, err := db.Exec("UPDATE cart SET items = JSON_ARRAY_APPEND(#items, '$', 'data') where id = 1")
I know this is not enough. How can I add my variable instead of 'data' to this query? Could anyone help me?
Thank you.
My guess would be a parameterized update:
db.Exec("UPDATE cart SET items = JSON_ARRAY_APPEND(#items, '$', ?) where id = ?", data, 1)
I have word which is dynamic "sticky9 uk". It might be anything.
I want to find similar record based on two columns
e.g
my_word = "sticky9 uk"
And in my database
Coloumn1 : company_name = "sticky9"
Coloumn2 : contact_person = "sticky9.com"
select * from your table where Coloumn1 like '%<Your Word>%' or Coloumn2 like '%<Your Word>%'
[major edit to make things clear]
I want to write a query that returns a dynamic column name like this:
SELECT
f2 AS
(
SELECT column_name
FROM column_names_tbl
WHERE column_name = "experience"
limit 0,1
)
FROM some_table
so that would output the same as this:
SELECT
f2 AS experience
FROM some_table
This is no correct SQL syntax, even because the two queries (the selected field and it's alias) are both subqueries and unrelated to each other. So, there's also no possibility for mysql to distinguish what name you want to connect to what value, even if the syntax was correct...
You already use a more or less normalized relational table, so I suggest the following solution:
you select the revision ID and name in a separate query; store them in PHP and use them for whatever you want
next, you evaluate the following query into a separated result set: SELECT ps.keyname, psv.keyvalue FROM page_setting_values AS psv INNER JOIN page_settings AS ps ON ps.id = psv.setting_id WHERE psv.page_revision_id = :revision with :revision representing your revision id
you may now assemble an associated array from that result set:
$settings = [];
$result = $db->executeQuery('...')->fetchAll();
foreach($result as $setting)
{
$settings[$setting['keyname']] = $setting['keyvalue'];
}
Hope that helps ;)