I have created Redis hash and stored below entries - 101- 104 are the hash keys
HMSET 101 field1 101 field2 yy field3 bb field4 300
HMSET 102 field1 102 field2 xx field3 bb field4 300
HMSET 103 field1 103 field2 yy field3 bb field4 300
HMSET 104 field1 104 field2 xx field3 bb field4 300
I want to retrieve all the records having field2 = xx (this is like where clause from SQL)
I think it should be done my creating secondary index on field2 but not sure how to write it.
Redis doesn't provide such functionality.
Also, Redis doesn't allow any indexes to be created.
Alternatively, it provides various data structures for you to save data according to your access needs eg: list, hashmap, set, sorted sets, etc
For your case, you'll have to manually iterate through all the records to get the desired results.
I think you should create a reverse index for your keys and value. i.e.
"value" ---> {{fieldName, HSETNAME}}
Ex : xx --> {{field2,101},{field3,104}}
Iterating over proper key,value you can get the HMSET name and then you can retrieve all the records from it.
It is just that you have to manipulate your keys and values according to your usecase.
You can find more info here: How do you search for keys with a value? For example get all KEYS where the value is "somevalue"
Related
number1= AnyNumber from 1 to 100
number2= AnyNumber from 1 to 100,
This is how my data looks in Splunk
{[-]
field1: number1,
fiedl2: number2,
...
}
I want to check if these two fields match or doesn't,
my Splunk Query
| search filed1 != field2
| stats count by field1,field2
Try using where with match:
<spl>
| where !match(field1,field2)
| stats count by field1 field 2
After adding the single quotes around the field2. I was able to get the data
for the list of fields that are not matching!
| where field1 != 'field2' | stats count by field1, field2
I have a list of integer from 1 to N elements (N < 24)
At the moment, there are two solutions to manage this value in a SQL database (I think it is the same for MySQL and Microsoft SQL Server)
Solution 1: use VARCHAR and , to separate integer values:
aaa | 40,50,50,10,600,200
aab | 40,50,600,200
aac | 40,50,50,10,600,200,500,1
Solution 2: create a new table with composite primary key (key, id) (id = index of element in list) and value:
aaa | 0 | 40
aaa | 1 | 50
aaa | 2 | 50
....
aab | 0 | 40
aab | 1 | 50
aab | 2 | 600
....
What is it better solution considering I have many items of data to load and I need to refresh this data many times
Thanks
Edit:
my operative case is: i need to refresh/read all data (list for key) with same call and i never call one by one, this is why i think first approach better.
And all math like avg or max i wanna do on client.
Usually the second approach is preferable. One advantage is ease of access:
-- Third value of aaa
select value from mytable where key = 'aaa' and pos = 3;
-- Avarage value of aaa
select avg(value) from mytable where key = 'aaa';
-- Avarage number of values
select avg(cnt) from (select count(*) as cnt from mytable group by key) counted;
Another is data consistency. You can add simple constraints to your columns, such as to allow only integers from, say, 1 to 700 and positions only up to 23.
There is an exception to the above, though. If you use the database only to store the list as is and you don't want to select separate values or even aggregate them, i.e. if this is just a string to the DBMS and your queries don't care about its content, then store it as a simple string. Why not?
The second solution that you propose is the classic way of doing this, I would recommend that.
The first solution is quite terrible in scaling and in other hundred things
I wonder which has better performance in this case. First of all, I want to show to the user his medical information. I have two tables
user
-----
id_user | type_blood | number | ...
1 O 123
2 A+ 442
user_allergies
-----------
id_user | name
1 name1
1 name2
I want to return:
JSON {id_user=1, type_blood=0, allergies=(name1,name2)}
So, Its better do a JOIN for user and user_allergies and iterate, or maybe two SELECT?
But if then I have another table like user_allergies, that the result can be:
user_another_table
-----------
id_user | name
1 namet1
1 namet2
1 namet3
JSON {id_user=1, type_blood=0, allergies=(name1,name2), table=(namet1,namet2,namet3)}
It's better three SELECT or a JOIN, but then I have to iterate on the results and I can't imagine a esay way. A JOIN can give me a result like:
id_user | type_blood | allergy_name | another_table_name
1 O name1 namet1
1 O name1 namet2
1 O name1 namet3
1 O name2 namet1
1 O name2 namet2
1 O name2 namet3
Is there any way to extract:
id_user | type_blood | allergy_name | another_table_name
1 O name1 namet1
1 O name2 namet2
1 O namet3
Thanks community, I'm newbie in SQL
Depending on the data - there is no way to get the 2nd set of results you've shown, if the 1st set of results shows the values. The 2nd one is throwing data away - in this case allergy 'name2' for another_table_name 'namet3'. This is why you get many rows back with repeated data.
You can use the group by clause to restrict this in some cases, but again - it won't let you throw away data like that.
You could try using the COALESCE clause, if your DB supports it.
If not, I think you're going to have to construct your JSON in some business logic, in which case its fine to read the data in a 3-way join. You order by the user id and either create or append the row data to the JSON document depending if a user record is present or not (if you order by user id, you only need to keep track of when the user id value changes).
Alternatively, you can read a list of users and single-item data in one query, and then ht the DB again for the repeating data.
If anyone can help
I have two tables like this:
field.tab1
111
1110
1111
1112
field.tab2
111
I need to update table 2 like this:
field.tab2
1113
Thank you for your quick response and guidance
Yes I'm new here and in sql, access too, I have those two tables that i need to compare and make the field from second table to have unique records because will be appended to the first table, so this is what i've done in access:
SELECT tab2.field, tab1.field, tab1.field1, tab2.field1
FROM tab2 LEFT JOIN tab1 ON tab2.field=tab1.field;
and manualy increase every tab1.field :) until tab2.field and tab2.field1 become null, where field is the number of client and field1 number of order.
The natural answer to this question is:
update field.tab2
set col = 1113
where col = 111;
My website attacked and hackers adds same string to all of my rows.
For example if the value of a row was "True value" they changed it to "True Value HACKED STRING ... HACKED by .. "
Unfortunately my site have lots of data and I can't change them one by one, but fortunately same data adds to all rows.
I want an SQL statement that removes all HACKED STRING from all rows.
You can do that quickly by removing the string with REPLACE function
Example TableA
id | Name
--------------
1 | Good HACKED BY XX
2 | We know HACKED BY XX
3 | Goodbye HACKED BY XX
Sql
UPDATE TableA
SET Name = REPLACE(Name, ' HACKED BY XX', '');
This will make you table look like this
id | Name
--------------
1 | Good
2 | We know
3 | Goodbye