Why do not order asc correctly? - mysql

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.

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.

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.

Store a set of numbers in 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

Get the SQL column Value dynamically

I have a column in table A. the column name is Sequence number. The Structure of table A is numbers from 1,2,3,4.....3600.
Now on the basis of table A. I want the below output from the SQL select query for SQL server 2008.
seq no dynamic col
1 1
2 1
3 1
4 1
5 1
6 2
7 2
8 2
9 2
10 2
11 2
12 3
13 3
My Second column is getting generated at the run time.
And the business logic is that, if the seq number mod 6 = 0 then increment the value of dynamic column.
Thanks in advance
Try this:
select seqno, (seqno/6) +1 dynamiccol
from t
Fiddle Demo
Take this as pseudo code because I'm not familiar with SQL Server specifically, but it should give you somewhere to go.
SELECT
seq_no,
ROUNDDOWN(seq_no/6)+1 AS dynamic_col
FROM
my_table

MySQL SELECT query - one cell to multiple rows

In MySQL I have one particular cell with data something like this
5,6,7,8,9
If I need to search for specific 2 numbers one after another I do a query with LIKE statement for those 2 particular numbers. For ex. I need to check if there's a row with numbers 6 & 7 *in a row* I do
SELECT * FROM table WHERE column LIKE '%,6,7,%' OR column LIKE '%,6,7' OR column LIKE '6,7,%'
It's little redundant and clumsy. If I 'convert' those numbers into multiple rows, for ex. every number would become it's own row with column 'numbers' ordered with 'sort' column so I know the order of rows.
id numbers sort
55 8 4
56 6 2
57 5 1
58 7 3
59 9 5
...
What's the identical query for this case? So I would have the same result as with the query above. I need to order the query with sort column and check if the numbers 6,7 are occurring one after another with that sorting.
Should be something like this. (if I understood right your problem). It will return nothing if the 2 numbers are not in sequence by the sort column.
select *
from table t1
join table t2 on t1.sort=t2.sort+1
where t1.numbers=6 and t2.numbers=7
if you do not know which one should be first you can use it like this:
select *
from table t1
join table t2 on t1.sort=t2.sort+1 or t1.sort+1=t2.sort
where t1.numbers=6 and t2.numbers=7
Are the numbery always incremented by one or can they have any value?
I'm proposing the following table structure
id col1 col2 rowid
1 1 2 1
2 2 3 1
3 1 4 2