Sample Header Ad - 728x90

Converting results to array

1 vote
2 answers
3511 views
I have many entities like: user
id | name
-----------
 1 | Joe
 2 | David
 3 | Jane
cars
id| name 
------------
1 | cars1 
2 | cars2 
3 | cars3 
4 | cars4 
5 | cars5 
6 | cars6 
7 | cars7 
8 | cars8 
9 | cars9
cars_data
id | price  | category | uid | car_id
---+--------+----------+-----+-------
1  | 225.00 |	p1 	   |  1  |	1
2  | 451.00 |	p2 	   |  1  |	1
3  | 324.00 |	p2 	   |  1  |	2
4  | 784.00 |	p2 	   |  1  |	3
5  | 724.00 |	p3 	   |  1  |	2
6  | 214.00 |	p1 	   |  2  |	1
7  | 451.00 |	p1 	   |  2  |	2
8  | 926.00 |	p1 	   |  2  |	3
9  | 271.00 |	p2 	   |  2  |	3
10 | 421.00 |	p2 	   |  2  |	4
11 | 684.00 |	p2 	   |  2  |	2
12 | 124.00 |	p3 	   |  2  |	5
13 | 128.00 |	p3 	   |  2  |	1
14 | 741.00 |	p1 	   |  3  |	1
15 | 965.00 |	p1 	   |  3  |	3
16 | 124.00 |	p2 	   |  3  |	4
17 | 415.00 |	p2 	   |  3  |	1
18 | 51.00 	|   p2    |  3  |	2
19 | 965.00 |	p2 	   |  3  |	6
filters
id 	| name 	  |  filter 	     | uid
----+ --------+------------------+-----
1 	| filter1 |	string filters 1 |	1
2 	| filter2 |	string filters 2 |	1
3 	| filter3 |	string filters 3 |	1
4 	| filter3 |	string filters 3 |	1
5 	| filter3 |	string filters 3 |	1
6 	| filter3 |	string filters 3 |	1
7 	| filter3 |	string filters 3 |	1
8 	| filter  |	string filters 1 |	2
9 	| filter5 |	string filters 5 |	2
10 	| filter6 |	string filters 6 |	2
11 	| filter6 |	string filters 6 |	2
12 	| filter6 |	string filters 6 |	2
13 	| filter6 |	string filters 6 |	2
14 	| filter7 |	string filters 7 |	3
15 	| filter8 |	string filters 8 |	3
16 	| filter8 |	string filters 8 |	3
17 	| filter8 |	string filters 8 |	3
18 	| filter8 |	string filters 8 |	3
19 	| filter9 |	string filters 9 |	3
assign_filters
uid | category  | filter_id
----+ ----------+-----------
1   |	p1 	    |1          
1   |	p2 	    |1          
1   |	p2 	    |2          
1   |	p2 	    |3          
1   |	p3 	    |4          
2   |	p1 	    |9          
2   |	p1 	    |8          
2   |	p1 	    |13         
3   |	p2 	    |14         
3   |	p2 	    |16         
3   |	p2 	    |17         
3   |	p3 	    |19         
3   |	p3 	    |18         
3   |	p1 	    |14         
3   |	p1 	    |18
What I want is results like this:
uid | category  | filter_id |car_id  
----+ ----------+-----------+--------
1   |	p1 	    |1          |[1] 
1   |	p2 	    |1          |[1,2,3]
1   |	p2 	    |2          |[1,2,3]
1   |	p2 	    |3          |[1,2,3]
1   |	p3 	    |4          |
2   |	p1 	    |9          |[1,2,3]
2   |	p1 	    |8          |[1,2,3]
2   |	p1 	    |13         |[1,2,3]
3   |	p2 	    |14         |[1,2,3,4,6]
3   |	p2 	    |16         |[1,2,3,4,6]
3   |	p2 	    |17         |[1,2,3,4,6]
3   |	p3 	    |19         |[6,7]
3   |	p3 	    |18         |[6,7]
3   |	p1 	    |14         |[1] 
3   |	p1 	    |18         |[1] 
How can I change the original SQL query to give me the result above? dbfiddle **update:** - Each user can define a filter for himself and each filter is specific to one user. - Each user can place cars in specific categories in the cars_data table.(categories is [p1,p2,p3]) - Each user can assign a number of filters to each of their categories. I need to know which filters each user has used, and on which cars the filter has been applied. For example, user number 1 has placed cars1, cars2, cars3 in category p1. Also, filter1, filter2, filter3 have been assigned to this category. The result I need is this:
uid | category  | filter_id |car_id  
----+ ----------+-----------+--------
1   |	p2 	    |1          |[1,2,3]
1   |	p2 	    |2          |[1,2,3]
1   |	p2 	    |3          |[1,2,3]
Asked by majid (15 rep)
Nov 1, 2021, 06:35 AM
Last activity: Apr 20, 2025, 04:56 AM