1

Based on the requirement I need to change logic. I have students table and columns like id and val. I want to write a select query.

Student table contains data:

id val
1 {"stdId":1,"stdName":"student","stdAddress":"testLoc","stdran":[1,2,3]}
2 {"stdId":2,"stdName":"student2","stdAddress":"testLoc","stdran":[2,3,4]}
3 {"stdId":3,"stdName":"student3","stdAddress":"testLoc","stdran":[1]}
4 {"stdId":4,"stdAddress":"testLoc","stdran":[]}
5 {}
6 {"stdId":5}

I want to show records where if stdran.size()>0 then I need to check whether 1 exists or not. if exits I need to throw that record and also I need to throw records like where val ={} and val doesn't contains stdran[] and if contains, it's stdran.size()=0

ex: if input :1
expecting output
id val
1 {"stdId":1,"stdName":"student","stdAddress":"testLoc","stdran":[1,2,3]}
3 {"stdId":3,"stdName":"student3","stdAddress":"testLoc","stdran":[1]}
4 {"stdId":4,"stdAddress":"testLoc","stdran":[]}
5 {}
6 {"stdId":5}

if input :4
expecting output
id val
2 {"stdId":2,"stdName":"student2","stdAddress":"testLoc","stdran":[2,3,4]}
4 {"stdId":4,"stdAddress":"testLoc","stdran":[]}
5 {}
6 {"stdId":5}

Can anyone help me on this, please?

0

3 Answers 3

1

I would use JSON_CONTAINS.. Check dbfiddle (mariadb_10.4)

syntax is JSON_CONTAINS(target, candidate[, path])

SELECT
    *
FROM
    `student`
WHERE
    JSON_CONTAINS(JSON_EXTRACT(`val`, '$.stdran'), '1') 
    OR JSON_EXTRACT(`val`, '$.stdran') IS NULL 
    OR JSON_EXTRACT(`val`, '$.stdran') = '[]'

And result For input 1 is enter image description here

For input 4 is

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Indra Kumar. I updated my question. if you can help me on this one, please?
1

I recently came up with the same problem and used these two approaches. See here: fetching records from a json object

1 Comment

if its still not clear you can ask me further in comments. PS: I was dealing with strings and your dealing with int values.
1

Simply use Json_Contains

CREATE TABLE table1 (
  `id` INTEGER,
  `val` JSON
INSERT INTO table1
VALUES
  ('1', '{"stdId":1,"stdName":"student","stdAddress":"testLoc","stdran":[1,2,3]}'),

  ('2', '{"stdId":2,"stdName":"student2","stdAddress":"testLoc","stdran":[1,2,3,4]}'),
  ('3', '{"stdId":3,"stdName":"student3","stdAddress":"testLoc","stdran":[1]}'),
  ('4', '{"stdId":4,"stdName":"student4","stdAddress":"testLoc","stdran":[2,3]}');
SELECT id FROM table1 WHERE JSON_CONTAINS(val,'1', '$.stdran') ; 
| id |
| -: |
|  1 |
|  2 |
|  3 |

db<>fiddle here

CREATE TABLE table1 (
  `id` INTEGER,
  `val` LONGTEXT
);
INSERT INTO table1
VALUES
  ('1', '{"stdId":1,"stdName":"student","stdAddress":"testLoc","stdran":[1,2,3]}'),

  ('2', '{"stdId":2,"stdName":"student2","stdAddress":"testLoc","stdran":[1,2,3,4]}'),
  ('3', '{"stdId":3,"stdName":"student3","stdAddress":"testLoc","stdran":[1]}'),
  ('4', '{"stdId":4,"stdName":"student4","stdAddress":"testLoc","stdran":[2,3]}');
SELECT id FROM table1 WHERE JSON_CONTAINS(val,'1', '$.stdran') ; 
| id |
| -: |
|  1 |
|  2 |
|  3 |

db<>fiddle here

5 Comments

val JSON or val longtext which one is right ?
that doesn't matter MySQL auomnatically converts it, see second example
I tried both, first example SELECT * gives some kind of encrypted values. But longtext is fine. Thats why i asked
Thanks @nbk . Your query working good. I have a one doubt. How can we retrieve where stdran=[] (empty array). and also want one more query where stdran[] not contains 1. can you help me on this please?
you know the rules one question only, accept and upvote the answer and ask a new one, but as a hint the NOT JSON_COntaINS for the latter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.