I have table region with fields
id | name | dates
Sample data
1 | local | "2018-01-01", "2018-01-02", "2018-01-03"
I want a query like this
SELECT * FROM region WHERE "2018-01-02" in (region.dates)
but this does not work. I do not use json data in this case. How can I change it?
(My)SQL doesn't work like that as it will see region.dates as a simple string. In which case, you can do SELECT * FROM region WHERE dates LIKE '%"2018-01-02"%';
However, a better solution would be to devolve that column into another table.
Related
My team has stored array data as a string in MySQL like below
["1","2","22","11"]
How can we select data from the table where the column contains a certain branch number.
Example of table
sno | Name | Branch
1. | Tom. | ["1","2","22"]
2. | Tim. | ["1","2"]
Can you suggest a query to select all rows containing branch 2?
We tried using FIND_IN_SET() but that is not working as the double quotes and square brackets are also a part of string.
Use like:
select *
from mytable
where Branch like '%"2"%'
I have a field with the value in a table like below (two records).
| field
|--------------------------------------------------------------------------------
| [{"id":"8a688d70-d881-11ea-b999-3b32356f3dce","supplierName":"t1"},{"id":"8a688d70-deeq-3221-cdee-3b32356f3dc1","supplierName":"t2"]
|--------------------------------------------------------------------------------
| [{"id":"8a688d70-323s-11ea-2123-3b32356f1111","supplierName":"t3"}]
|--------------------------------------------------------------------------------
| ...
When I use the SQL
select substring(field1, 9, 36)
FROM table
I get the records
8a688d70-d881-11ea-b999-3b32356f3dce
8a688d70-323s-11ea-2123-3b32356f1111
Now, Is there any way to get all the id value in a record?
NOTE, maybe one record had mutiple id values.
Ideal result should be like below.
|————————————————————————————————————————————————————————————————————————————
|8a688d70-d881-11ea-b999-3b32356f3dce, 8a688d70-deeq-3221-cdee-3b32356f3dc1
|————————————————————————————————————————————————————————————————————————————
|8a688d70-323s-11ea-2123-3b32356f1111
|————————————————————————————————————————————————————————————————————————————
Use JSON_EXTRACT of mySql.
SELECT JSON_EXTRACT(field, "$[*].id") AS field FROM table
I have a table named 'items'.
it has columns including index column such as below.
title | name | areacode
---------------------------------
police | user1 | 31,31,31
FireStation | user2 | 31,1,2
Restaurant | user22 | 1,1,0,32,32
---------------------------------
when i use below statement
select title, name from items where IN(31)
i get (police,user1) and (FireStation,user2)
However when i use IN(1)
i cannot get (FireStation, user2)
i found out that IN clause is useful when multiple values such as
IN(31,1)
are used.
But when single value such as IN(1) or IN(0), it sometimes does not fetch data correctly. I have found out CONTAINS method. however not familiar with it.
In sum, how can i fetch (FireStation, user2) if areacode contains a value '1'? or (Restaurant, user22) when areacode has a value '0'?
You should probably not be storing your area code data as CSV, because it is unnormalized and therefore will be hard to work with. That being said, MySQL has a function called FIND_IN_SET() which can check if a given value appears in a CSV string. Something like this should work:
SELECT title, name
FROM items
WHERE FIND_IN_SET('0', areacode) > 0
If you wanted to check for both the 0 or 1 area code, you could use this WHERE clause:
WHERE FIND_IN_SET('0', areacode) > 0 OR FIND_IN_SET('1', areacode) > 0
I am having problem with storing the data in mysql. I want to save the data in an unordered way inside a database. for eg.
Number | Name | Section | Grades |
1 | x | A | 80% |
3 | z | B | 72% |
2 | y | C | 55% |
I want to save the data inside the database in such a way that the data is saved according to the number in order. It should be stored in order of number.
I saw the function GROUP BY which show the data in an arranged way. But I need to save the data in database file that way. Is there any way by which I can make the data in order inside database file.?
Thanks for your time.
this one is will going to display in grades format like as,3,2,1 :
SELECT number,name, section, grades FROM tablename ORDER BY grades DESC;
The order in which the data is save is not relevant to you as you can use the ORDER BY operator to order your select results.
An example query by sorting the data will be:
SELECT * FROM <table> ORDER BY name ASC;
You can see the full documentation on:
https://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html
Reading the comments, a view may be usefull for you, so you can access the data in an ordered way from the other software:
CREATE VIEW <view_name> AS SELECT * FROM <table> ORDER BY name ASC;
And the you can access the view with
SELECT * from <view_name>
and your data will be in the order you defined in the view query.
You can find the full view reference on:
https://dev.mysql.com/doc/refman/5.0/en/create-view.html
Is there a way I can store multiple values in a single cell instead of different rows, and search for them?
Can I do:
pId | available
1 | US,UK,CA,SE
2 | US,SE
Instead of:
pId | available
1 | US
1 | UK
1 | CA
1 | SE
Then do:
select pId from table where available = 'US'
You can do that, but it makes the query inefficient. You can look for a substring in the field, but that means that the query can't make use of any index, which is a big performance issue when you have many rows in your table.
This is how you would use it in your special case with two character codes:
select pId from table where find_in_set('US', available)
Keeping the values in separate records makes every operation where you use the values, like filtering and joining, more efficient.
you can use the like operator to get the result
Select pid from table where available like '%US%'