storing allowed settings combinations - mysql

I am making an interface where a user can define settings. The settings are separated in categories and the user can only select one parameter from each category. The thing is, only certain combinations of parameters are allowed and I must prevent the users from selecting incompatible parameters.
I am trying to design the mysql database structure for that but I did not find a solution that satisfies me.
What I thought of is :
Categories
- ID
- Description
Parameters
- ID
- Parent Category ID
- Description
Combinations
-ID
- a string that is the concatenation of parameters IDs ordered by category
eg. : 102596 would be the combination of parameters 10 from category 1, 25 from category 2, and 96 from category 3.
The problems is : what if one day I need more than a hundred parameters ?

Ok So you have these tables:
Categories
- ID
- Description
And each category has multiple parameters:
Parameters
- ID
- Parent Category ID
- Description
Now you want to store the parameters that the user has selected. Why dont you handle the operation of permitting of selecting only one parameter per category outside of the database design scope? If you can do that, then the combination table would be simple:
Combinations
- ID
- USER_ID
- CATEGORY_ID
- PAREMETER_ID

You should probably use a table for grouping combinations, as such:
Combinations
- ID
- Group_ID
- Parameter_ID
So for instance, let's use your example of the allowed combination 10, 25 and 96, you would have this 3 entries in the "Combinations" table:
(ID, Group_ID, Parameter_ID) = (1, 1, 10), (2, 1, 25), (3, 1, 96)
(the IDs can be auto-generated ones)
So that means you have this group (1) of 3 allowed parameters (10, 25, 96).
then if you want to add another possible combination of, say, 15, 16, 23 and 42:
(ID, Group_ID, Parameter_ID) = (4, 2, 15), (5, 2, 16), (6, 2, 23), (7, 2, 42)
(again the IDs can be auto-generated ones)
So that means you have this group (2) of 4 allowed parameters (15, 16, 23, 42).

Related

How to use where clause with given numbers and show the person which rows contains given number

Given that I have four numbers: 2,4,6,8 to search in each row. If the row contains or matches the given numbers orderly or in any order it will show the person's name. I've tried some ways but unlucky.
Here is the screenshot of my data table:
https://pasteboard.co/HYs6pAi.png
Select Person from tblluckynumber where num1=2 and num2=4 and num3=6 and num4=8
The output should show the Person's name in Query: Ken and Josh because they matches the four given numbers
SELECT Person
FROM tblluckynumber
WHERE num1 IN (2, 4, 6, 8) AND
num2 IN (2, 4, 6, 8) AND
num3 IN (2, 4, 6, 8) AND
num4 IN (2, 4, 6, 8)

SQL: find intervals of step function

I have a table with 2 columns:
id INT PRIMARY_KEY
y FLOAT
The value in column "y" is guaranteed to be in 2 ranges:
small values: [0.18 - 0.20]
big values: [2.3 - 2.4]
It is known what "y" column in table has the following pattern: several records with small values then several records with big values then again several small and several big and so on. The number of consecutive records with the same range is not known and it is not fixed (vary). I need to find the id's (PK column) for beginning and ends of all intervals. Is it possible to do it in SQL?
(1; 0.19) (2; 0.18) (3; 0.19) (4; 2.3) (5; 2.4) (6; 2.3) (7;0.19)
Expect output: (1, 'start of small'), (3, 'end of of small'), (4, 'start of big'), (6, 'end of big'), (7, 'start of small')

MySQL get all rows in which one field belongs to array, and all members have a common value in the other field

Let's say I have the following table of user properties:
id, user_id properties
1, NULL, prop_ss1
2, NULL, prop_ss2
3, 2, prop_1
4, 2, prop_2
5, 3, prop_1
6, 3, prop_2
7, 3, prop_3
8, 4, prop_1
Given an array of user_ids, how could I get the list of all properties which either have the user_id NULL (call it a global property if you wish), or are shared among all user_ids in the given array?
For instance, given an array (2,3), I would like to get:
prop_ss1
prop_ss2
prop_1
prop_2
Or, given an array(2,3,4), I would like to get:
prop_ss1
prop_ss2
prop_1
Try a UNION of two separate queries:
SELECT properties FROM your_table WHERE user_id IS NULL
UNION
SELECT properties
FROM your_table
WHERE user_id IN (2, 3)
GROUP BY properties
HAVING COUNT(DISTINCT user_id) = 2
See it working online: sqlfiddle
The number 2 in the last line is the number of users that you are querying for.
select distinct properties from table
where user_id is null
or user_id in (1,2,3)
Sorry misread your post, need group by and having

How to Check a Table with column "nid" for multiple values and delete them if they are present

I want to check a Column with title nid in my table in Mysql called node
I have a list of values say 17, 21 , 45, 48
I can get the following code to find the row with nid being equal to 17 using the code below
DELETE FROM node WHERE nid = 17
how should i edit it to check for all values = 17,21,45,48.
Use the IN clause:
DELETE FROM node WHERE nid IN (17, 21, 45, 48)

MySQL - How to query items by tags, order by matched tag count

My schema looks something like this:
items ( id, title, blah )
tags (id, name )
item_tags ( item_id, tag_id )
I want to list all items, where the item's tags are "in" an array of selected tags, and then order by the number of tags that match the selection (e.g. [1, 2, 3])
What I have so far is:
SELECT *, COUNT(item_tags.tag_id) AS tag_count
FROM items
JOIN item_tags
ON item_tags.item_id = items.id
WHERE item_tags.tag_id IN (1, 2, 3)
GROUP BY items.id
ORDER BY tag_count DESC
This works well, except the tag_count just gets the total number of tags for the item selected, i want it be the number of tags selected that are contained in (1, 2, 3).
An item with tags (1, 2, 3) should come before an item with tags (1, 5, 6, 7).
I am using Kohana 3's ORM if there is a solution that way.
Simply change your SELECT to:
SELECT *, COUNT(*) AS tag_count
.....
I think what you really want is a GROUP_CONCAT( tag_id ) (maybe in addition to your count). The group concat will concatinate your IDs such as you've shown... 1, 2, 3 and 1, 5, 6, 7. If you use THAT column as your order by, you should be good.
Now, that said, you might have to force some formatting of the concatination process to handle things like 1, 10, 100 coming before 1, 2, 3. As such, if formatting the concatinated ID strings to say... 2 or 3 positions, you would get
001, 002, 003
vs
001, 005, 006, 007
vs
001, 010, 100
HTH