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!
Related
Environment :
MySQL 5.7.x
Spring MVC
Table Data (name: TableA)
seq
level
name
order
parent_seq
1
1
name1
1
0
2
1
name2
2
0
3
2
sub1-1
1
1
4
2
sub1-2
2
1
5
2
sub2-1
1
2
6
3
third-2-1
1
5
7
3
third-1-1
1
3
Expected Result
seq
level
name
order
parent_seq
next_level
1
1
name1
1
0
2
3
2
sub1-1
1
1
3
7
3
third-1-1
1
3
2
4
2
sub1-2
2
1
1
2
1
name2
2
0
2
5
2
sub2-1
1
2
3
6
3
third-2-1
1
5
1 (last default value: 1)
Now I'm genenrating expected result with nested for statement(JAVA).
Is there any way to generate expected result only with MySQL Query?
The data stacked in random order in the table is sorted by ASC based on the level column, but check the parent_seq column so that it is sorted under the parent data. And if there are multiple data of the same level, sort by ASC based on the sort column value.
Thanks in advance!
++
EmbraceNothingButFuture's answer was great, but the query seems to work on MySQL 8. I'm using MySQL 5.7. Is there any way to use the query on MySQL 5.7?
Summary:
Use REGEXP_SUBSTR(name,"[0-9]+\-?[0-9]*") to extract the numbers and sort the datas using the numbers.
For MySQL v8 above, you can use LEAD() to generate the "next_level" column based on the "level" column
COALESCE() function for the last default value = 1
SELECT
t1.*,
COALESCE(LEAD(t1.level, 1) OVER(ORDER BY REGEXP_SUBSTR(name,"[0-9]+\-?[0-9]*")), 1) AS next_level
FROM TableA t1
ORDER BY REGEXP_SUBSTR(name,"[0-9]+\-?[0-9]*"), t1.level
See db<>fiddle
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.
Suppose I have table called tree with id, and parent-id and data are b-tree
1
2 3
4 5 6 7 8 9
etc
all these are store in tree table like
1 - null
2 - 1
3 - 1
4 - 2
5 - 2
6 - 2
7 - 3
8 - 3
9 - 3
etc
so please write Query to fetch child tree of 2
output should be like
4 - 2
5 - 2
6 - 2
xx - 4
etc..
Select id, parent_id from tablename where parent_id = 2. Perhaps you need bottom level ones? Then you need to left join the table with itself and only return the rows that don't have children.
Edit: if the maximum number of hierarchy levels is high or unknown, you should use hierarchyid: https://msdn.microsoft.com/en-us/library/bb677213.aspx
Hope this helps!
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 ?
I need to filter a table in mysql but can't get past the beginning.
The table has 2 fields:
ID_house house_feature
1 1
1 2
1 4
1 5
2 1
2 3
2 4
3 1
3 2
3 3
I need to filter this table using the following parameters:
house feature = 1
AND
house feature = 2
AND
house feature = 3
So that I get all houses with the requested feature.
I already tried to create something similar to this:
SELECT *
FROM houses
WHERE
house_feature = 1
AND
house_feature = 2
AND
house_feature = 3
But it doesn't work as I expected.
Is there a way to get this result with MySQL?
It seems that I acn filter the table using only the OR operator but this way I can't get the right result.
Thanks in advance for any help.
tony
You can do so ,by matching the distinct count of features per house ,so the house with exactly these 3 features will be returned
SELECT *
FROM t
WHERE
house_feature IN(1 ,2,3)
group by ID_house
having count(distinct house_feature) = 3
Demo