Generate numbers based on a table column mysql - mysql

I have a table with the following rows:
ID Description Number
1 Test 1 4
2 Test 2 3
3 Test 3 5
4 Test 5 6
How do I create my query so that if I want ID 3, it generates the following based on the Number column:
Count
1
2
3
4
5
Thanks. :)

It looks like your rows are already uniquley identified. You need to query the row with id 3, and then preform an operation with php to count out to the end of the number set. 5 in this case. You could use arrays, and just loop throjgh the array for each number as well.

Related

Resequence column numbers in MySQL

I have a table that has a column with numbers in a sequence, but for some numbers they can be in groups with several of the same number. Then when the user deletes out a group, there can be a gap in the numbers.
What I want to do is re-sequence that column so it starts at 1, then proceeds up to the last column. So for example, the table column named Item could look like this:
1
1
2
3
5
5
7
8
8
and I want it to convert to this:
1
1
2
3
4
4
5
6
6
Is there a way to do this in MySQL?
I wouldn't encourage to do this, but you can re-number your values after each update
set #cnt = 0;
update test t set t.number=#cnt:=#cnt+1;
Update: this will increment the number field by one

group by based on two columns with same but vice versa values

What I Have:
I have a table where I have two basic columns on which the query is supposed to work
c1|c2
1 2
2 1
1 3
3 1
2 4
Now I want the result after the execution of the query is this
c1|c2
2 1
3 1
Notice
(3,1) was the last occurence of 1 and 3 no matter in which order they are as along as it is 1 and 3 and same is (2,1)
How can I achieve this where one value of the two columns is static for example in this case (1) is static and I want only those rows which has (1) in any of the two columns in their last occurrence.. Can any one help ?

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 query for alternatives

I have two tables:
Part: id, partnumber
Alternative: partid, alternativepartid
Let's say that Part contains the following records:
1 - part 1 2
2 - part 2
3 - part 3
4 - part 4
5 - part 5
And Alternative contains the following records:
1 - 2
1 - 3
2 - 4
3 - 5
I would like to create a query to get all parts that are related to a given id.
In my case that would mean that if I query for part 1 (id 1) I would like to find:
2 and 3 but also 4 and 5 as 4 is an alternative to 2 and 5 is an alternative to 3.
When I query for 3, I would like to find: 5, 1, 2, and 4
Does this make sense?
Would it be possible to retrieve the complete set of records in one query?
Thanks!

Is this pattern reconstitution or what is the name for this problem?

I've following problem and don't know the terminology to describe it and hence search for possible solutions.
I have a pivot table (matrix), eg each row and column have a named header. there is a defined set for rows and columns. Now let's assume that 10 rows are "combined" meaning each column is summed up to create a new "pattern".
What I would like is a way to determine alternative row combinations that lead to the same or similar "combined" pattern.
1 1 1
5 5 5
"Combined"
6 6 6
alternate row combination:
2 2 2
4 4 4
Suggestions? How is this problem called?
http://en.wikipedia.org/wiki/System_of_linear_equations#Matrix_equation
I just have to transpose above matrix to get Matrix A
[code]
1 5
1 5
1 5
[/code]
combined matrix is a vector b:
[code]
6
6
6
[/code]
and x would be just a vector full of 1.