I have a Table A
id
something
jsonModel
jsonModel could look like this
{
"text":"foo",
"subkey1":{
"entity":{
"name":"foo",
"customId":"59d61ffcf5bcb11f250d73275a252b62624eac000180ab59"
}
}
}
I'm trying to select a row from table A when a certain ID is contained in the the jsonModel.
I have tried this but it does not work saying I'm not allowed to use wildcards in the expression
SELECT
*
FROM A
WHERE json_contains(jsonModel,'59d61ffcf5bcb11f250d73275a252b62624eac000180ab59', $**.customId)
Is there a way to achieve this knowing that I do not know where in the model customId can be?
EDIT:
I ended up writing this which works but I don't know which solution between this or wchiquito is best
SELECT *
from A WHERE
JSON_CONTAINS(JSON_EXTRACT(jsonModel, '$**.customId'), '"59d61ffcf5bcb11f250d73275a252b62624eac000180ab59"');
Try:
SELECT
`id`,
`jsonModel`
FROM
`A`
WHERE
JSON_SEARCH(
`jsonModel`,
'one',
'59d61ffcf5bcb11f250d73275a252b62624eac000180ab59',
NULL,
'$**.customId'
) IS NOT NULL;
See db-fiddle.
Even tough its not correct or best approach but it works,
SELECT * from `A` where `jsonModel` like '%customId%59d61ffcf5bcb11f250d73275a252b62624eac000180ab59%'
Related
I know in other languages you can use something (usually a !) to denote the opposite of whatever follows. Is there a way to do something like this in MySQL?
For example, I have this query to select everything that isn't null or blank:
select * from `invoices` where `Notes`>'';
But is there a way way to do the opposite? Because currently I only know that you can rewrite the query such as this
select * from `invoices` where ifnull(`Notes`,'')='';
But this removes the opportunity for indexes on the Notes column or this way
select * from `invoices` where (`Notes` is null or `Notes`='');
But that one is a lot longer than something like
select * from `invoices` where !`Notes`>'';
which would be ideal.
SQL has a not operator:
SELECT * FROM `invoices` WHERE NOT (`Notes`>'');
-- Here -----------------------^
SELECT REPLACE(GROUP_CONCAT(DISTINCT(myValue)),',','|') FROM myTable WHERE myconditions
I'm using this query multiple times in a union. When myValue is different from NULL, it works. But when it has no value, the query fails (it says the query can't be executed). I tried to do IF(myValue IS NULL, '000', myValue), it doesn't work (Same for IFNULL). I think the distinct is what doesn't work here, since this query :
SELECT DISTINCT('000') FROM myTable WHERE myconditions
doesn't work either.
How can I manage error for when myValue is null with a GROUP_CONCAT(DISTINCT())?
Thank you
Try out this:
SELECT REPLACE(GROUP_CONCAT(DISTINCT(coalesce(myValue,'000'))),',','|')
FROM myTable WHERE myconditions
I have looked through questions here and have not been able to find exactly what I am looking for.
How would I create a query in mysql that would return missing data in field Name in database Demo as a string (such as 'Blank') instead of NULL?
I really appreciate your help!
SELECT IFNULL(fieldname, 'Blank') FROM tablename
or
SELECT COALESCE(fieldname, 'Blank') FROM tablename
Yes, in MySQL you can use IFNULL, as in:
SELECT IFNULL(fieldname, 'Blank') as fieldname, ...
I'm not really sure what you're aiming at, but maybe you look for something like this?
SELECT IF(ISNULL(a.yourfield),'Blank', a.yourfield)
FROM yourtable A;
ISNULL() returns 1 if the argument is NULL, otherwise it returns 0.
I'm trying to put together a MYSQL query designed for an AJAX\PHP CMS, which goes a little like this:
SELECT table.info
FROM table
WHERE table.variable LIKE '%refinedby%' IN
(SELECT other valid subquery to select data from)
However, I keep tripping syntax errors near LIKE '%refinedby'IN (
If I use = rather than LIKE there's no problem, as the following:
SELECT table.info
FROM table
WHERE table.variable = 'refinedby' IN
(SELECT other valid subquery to select data from)
Does anyone have any ideas where I can't preceed a subquery with a LIKE selector?
Thanks in advance!
You can't use in like that you have to specify what is going to BE in
SELECT table.info
FROM table
WHERE table.variable LIKE '%refinedby%'
and table.variable IN (SELECT other valid subquery to select data from)
First off, I know this is impossible as of MySQL 5.1.x, it says so right here:
http://dev.mysql.com/doc/refman/5.1/en/update.html
What I'm asking though is if there is a clever way to execute a query using either a temporarily table/view without having to resort to writing a quick script to do the work. My query (which is COMPLETELY WRONG and DOES NOT work, just an FYI for folks trying this at home) looks something like this:
UPDATE some_table
set some_col = ( SELECT some_othercol
from some_table
WHERE some_col > some_othercol
);
I'm trying to ultimately set some_col to the value of some_othercol if sol_col > some_othercol.
What's the best way to handle this without resorting to a script?
EDIT
My subquery returns more than one row!
I don't really understand why you need a subquery. Doesn't this do what you want?:
UPDATE some_table
SET some_col = some_othercol
WHERE some_col > some_othercol
I'm not sure if this is quite what you're trying to do, but maybe I can show you something to put you on the right path.
UPDATE some_table ST
SET some_col = (
SELECT some_col
FROM some_table OT
WHERE OT.ID = ST.ID
)
WHERE ST.some_col > 2
That will set some_col to its own value, but only when some_col is already greater than 2. I know this doesn't do anything, but it shows a concept that may be closer to what you're looking for. If you give some more detail as to what you what to happen in the end, I can possibly help you find a solution closer to what you need.
http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/ is the best resource I know on that subject.