Store a set of numbers in mysql - mysql

I need to store a set of integers in MYSQL. The problem is as : I have an id(integer). Each id is mapped to a set of numbers. The numbers in the set can have values from 1 - 160. So my table should be of structure {id,set}. Also each id'set can have different length (max 40 numbers).
Eg:
id set
1 {2 45 46 67 89 155}
2 {1, 11 12 34 56 67 68 79 80 134 145}
I have tried the SET datatype in MYSQL. I defined it as
CREATE TABLE mytable ( id NUMBER PRIMARY KEY , mycol SET('1','2','3','4'...))
But the problem is the set allows to define only 64 elemnets.
Note: I need options other than creating a mapping table as :
id setno
1 2
1 45
....... and so on
Can anyone suggest another way to store a set of numbers in MYSQL.

Create child table to hold the numbers, one per row:
create table set_number (
set_id int,
num int
);
Then insert your numbers. To get them as one CSV value, use group_concat(), something like:
select t.id, group_concat(s.num) set
from mytable t
join set_nimber s
on t.set_id = s.set_id
group by t.id

The table structure you described is the right way to do.
And its all in the way you present data, create a table where it maps ID into Sento
And ID is a primary key
So that you will have:
ID Sento
1 2
1 45
1 46
...
1 155
And then have an SQL that says:
SELECT ID, GROUP_CONCAT(Sento, SEPARATOR ', ')
FROM Table
GROUP BY ID
Hope this helps

Related

Create new column storing average of rows from another column in MySQL

I am trying to create a new column that has stores the 'Average weight of the field'. For example, the answer for RaceID = 123 would be 54.5. The RaceID's are not organised from smaller to largest and are displayed randomly like the example below.
RaceID
Weight
No. Starters
123
56
2
124
58
2
123
53
2
125
60
2
125
51
2
124
62
2
Try below query, It will display current table data along with average column :
select t.*,
avg(Weight) over(partition by raceID order by raceID ) avg_raceID
from table t;
SELECT RaceID, AVG(Weight) AS val_1
FROM dataset_name
GROUP BY RaceID;
By using above code we can get the average value of weights for every unique RaceID. Check the below image for better understanding.
https://i.stack.imgur.com/kMA68.png
Let me know if there are any modifications or error.

storing values of resultset into different table based on condition

I have a table 'new_table' in mysql, I want to store the result into two different tables based on a condition i.e if the percentage is above 70 then the result should be stored in dominant else should be stored in others
from below table the result based on condition(if percentage > 70) should store values 80 and 75 in dominant table and should store 20 , 40 , 60 in others table
kindly help.
thanks in advance.
sku_id new_total percentage
1 8 20
2 12 40
3 14 80
4 10 75
5 13 60
You can use create as select command like this:
CREATE TABLE dominant AS (
SELECT * FROM new_table
WHERE percentage > 70)
And for the second table same logic:
CREATE TABLE others AS (
SELECT * FROM new_table
WHERE percentage < 70)

Why do not order asc correctly?

I have in Sql a column name like that:
SpecificationParagraph
1
2
3
4
5
...
179
With my program i insert between row 1 and 2 a new row. My column are now :
SpecificationParagraph
1
3
4
5
...
179
2
When i try to order asc like that:
SELECT SpecificationParagraph
FROM CP_Sequence
ORDER BY SpecificationParagraph
My column get that order:
1
10
100
101
102
...
99
I want to order form 1 to 180. My logical to my program in vb is: when I insert a new row like 2, replace current 2 from SpecificationParagraph and after increment +1 all following lines.
you have to change datatype of the field to int, tinyint or bigint
You have probably created SpecificationParagraph field as a varchar or text, therefore the order by dorts it as text, not as a number. Change the data type to an integer type and the ordering will be fine.

how to patabless result of mysql stored procedure inside temporary

I have one stored procedure with one in parameter that returns multiple rows with 3 different columns. I want this result to be inserted inside another table with these 3 column filed and other tables individual fields.
However, when I create the temporary table it shows a blank resultset.
Can anybody help me to sort out this?
CREATE DEFINER=`root`#`localhost` PROCEDURE `getContactRolePermission`(
in contactroleid double
-- inout PermissionTableID double
)
BEGIN
select -- entitydetails.id,
contact.id as userid,
-- contacttyperolemap.contacttypeId ,
contacttyperolemap.roleid ,rolepermission.permissionid
from contacttyperolemap
join rolepermission on contacttyperolemap.roleid=rolepermission.roleid
join entitydetails on entitydetails.id = contacttyperolemap.ContactTypeID
join contact on contact.contactTypeID = entitydetails.id
where contacttyperolemap.contactTypeID = contactroleid
order by contact.id;
CREATE temporary TABLE tmp(
ID double ,
userid double ,
roleid double ,
permissionid double
);
END
stored procedures result is
uid rid pid
2 1 5
2 1 2
2 1 3
2 1 4
2 1 1
23 1 4
23 1 1
23 1 5
23 1 2
23 1 3
26 1 4
26 1 1
26 1 5
26 1 2
26 1 3
I want to insert this data inside userpermission table
schema is
id uid rid pid
1 10 2 1
Looks like you need to create a view instead of a stored procedure for the first 3 rows. Then in a separate query you can query any columns from that view and join that data with whatever other data you want from other tables. Hopefully I read the question right. Heres an article on creating views: https://msdn.microsoft.com/en-us/library/ms187956.aspx
Use INSERT ... SELECT command to fill target table.
And, your temp table shows empty result, because you have not filled it.

Oracle query issue in implementing logic for checking on million records

I have a table A
code_Value key_value Description
12 12 Entry_Category5
13 rrtt Entry2
20 tht Entry6
20 trt Wntry9
Table A has similar ways million records..
A logic is implemented in Table B which uses Table A as source
Code_value Key_value Description
12 12 Entry_Category5
13 rrtt Entry2
13 13 Null value
20 tht Entry6
20 trt Entry9
20 20 Null value
The logic is, in table A if i have an entry, where code is not equal to key then a new entry of my previous code will be copied with key as the code,description must be null.
This logic must be applied to million records.I just want to have an sql query which will
help me.Please suggest since there are more records
Try:
insert into table_b
select
code_value,key_value,description
from
table_a
where
code_value = key_value
union
select
code_value,code_value,null
from
table_a
where
code_value != key_value