I have a table with two columns. First column is the parent category whereas the second column is the sub category of the first column.
In this example: the layer will go something like this.
cat_id(1) -> sub_cat_id(4) -> then sub_cat_id(4) has four more subsub categories (235, 236, 237, 310) and each one of the subsub category can have more subsubsub category and so on.
I'm look for sql statement to help
a) cat_id doesn't have sub_cat_id
b) cat_id that has sub_cat_id
c) more importantly, a way to retrieve all the sub, subsub, subsubsub,.... for each parent category
Thank you.
cat_id sub_cat_id
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 240
1 315
4 235
4 236
4 237
4 310
5 238
5 239
6 230
6 231
6 232
6 233
6 234
7 175
7 176
7 177
7 227
7 228
7 229
7 281
9 159
9 160
9 161
9 162
11 304
11 305
13 222
13 223
13 224
13 225
13 226
19 173
19 174
19 241
19 252
19 316
19 317
20 21
20 22
20 23
20 24
20 25
20 26
20 27
28 30
28 32
28 34
28 36
40 41
40 42
40 43
40 44
40 45
40 46
40 47
40 48
40 49
40 50
40 51
40 52
40 53
40 54
40 55
40 56
40 303
40 319
47 274
47 275
49 154
49 155
49 156
49 157
49 158
57 58
57 64
57 65
57 66
57 72
57 73
57 80
57 253
57 312
57 313
58 59
58 60
58 61
58 62
58 63
58 191
58 193
58 278
64 199
64 200
64 201
64 202
64 203
64 204
64 311
65 195
65 196
65 197
65 198
66 205
66 206
66 276
67 68
67 69
67 70
73 74
73 178
73 179
81 82
81 83
81 84
81 85
81 86
82 261
82 262
82 283
82 285
83 211
83 212
87 88
87 89
87 90
87 91
87 92
87 93
87 94
87 95
87 96
87 272
87 277
97 98
97 99
97 100
97 101
97 103
97 217
97 295
98 300
98 301
98 302
99 213
99 214
99 215
99 216
99 271
99 282
99 309
99 314
101 102
101 182
101 183
101 184
101 185
101 186
101 187
101 279
101 288
105 106
105 107
105 108
105 109
105 110
105 111
105 112
105 113
105 114
105 192
105 194
105 270
105 284
105 289
105 293
105 318
113 263
113 264
113 265
113 266
115 116
115 117
115 118
115 119
115 120
115 121
122 123
122 124
122 125
122 127
122 128
122 129
122 130
122 131
122 132
122 243
122 273
125 126
125 180
125 181
125 299
130 296
130 297
130 298
133 134
133 135
133 136
133 146
133 147
133 148
133 150
133 151
133 247
136 137
136 138
136 139
136 140
136 141
136 143
136 144
136 248
136 290
136 291
136 292
147 163
147 164
147 165
147 166
147 167
147 168
147 169
147 267
147 286
147 287
148 170
148 171
148 172
151 149
151 268
151 269
218 219
218 220
242 67
242 71
242 249
242 251
247 244
247 245
247 246
249 76
249 208
249 209
249 250
249 306
249 307
249 308
251 77
251 79
251 210
251 280
254 31
254 33
254 37
254 38
254 39
254 104
254 256
254 257
In this example: the layer will go something like this.
cat_id(1) -> sub_cat_id(4) -> then sub_cat_id(4) has four more subsub categories (235, 236, 237, 310) and each one of the subsub category can have more subsubsub category and so on.
I'm look for sql statement to help
a) cat_id doesn't have sub_cat_id
b) cat_id that has sub_cat_id
c) more importantly, a way to retrieve all the sub, subsub, subsubsub,.... for each parent category
Thank you.
cat_id sub_cat_id
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 240
1 315
4 235
4 236
4 237
4 310
5 238
5 239
6 230
6 231
6 232
6 233
6 234
7 175
7 176
7 177
7 227
7 228
7 229
7 281
9 159
9 160
9 161
9 162
11 304
11 305
13 222
13 223
13 224
13 225
13 226
19 173
19 174
19 241
19 252
19 316
19 317
20 21
20 22
20 23
20 24
20 25
20 26
20 27
28 30
28 32
28 34
28 36
40 41
40 42
40 43
40 44
40 45
40 46
40 47
40 48
40 49
40 50
40 51
40 52
40 53
40 54
40 55
40 56
40 303
40 319
47 274
47 275
49 154
49 155
49 156
49 157
49 158
57 58
57 64
57 65
57 66
57 72
57 73
57 80
57 253
57 312
57 313
58 59
58 60
58 61
58 62
58 63
58 191
58 193
58 278
64 199
64 200
64 201
64 202
64 203
64 204
64 311
65 195
65 196
65 197
65 198
66 205
66 206
66 276
67 68
67 69
67 70
73 74
73 178
73 179
81 82
81 83
81 84
81 85
81 86
82 261
82 262
82 283
82 285
83 211
83 212
87 88
87 89
87 90
87 91
87 92
87 93
87 94
87 95
87 96
87 272
87 277
97 98
97 99
97 100
97 101
97 103
97 217
97 295
98 300
98 301
98 302
99 213
99 214
99 215
99 216
99 271
99 282
99 309
99 314
101 102
101 182
101 183
101 184
101 185
101 186
101 187
101 279
101 288
105 106
105 107
105 108
105 109
105 110
105 111
105 112
105 113
105 114
105 192
105 194
105 270
105 284
105 289
105 293
105 318
113 263
113 264
113 265
113 266
115 116
115 117
115 118
115 119
115 120
115 121
122 123
122 124
122 125
122 127
122 128
122 129
122 130
122 131
122 132
122 243
122 273
125 126
125 180
125 181
125 299
130 296
130 297
130 298
133 134
133 135
133 136
133 146
133 147
133 148
133 150
133 151
133 247
136 137
136 138
136 139
136 140
136 141
136 143
136 144
136 248
136 290
136 291
136 292
147 163
147 164
147 165
147 166
147 167
147 168
147 169
147 267
147 286
147 287
148 170
148 171
148 172
151 149
151 268
151 269
218 219
218 220
242 67
242 71
242 249
242 251
247 244
247 245
247 246
249 76
249 208
249 209
249 250
249 306
249 307
249 308
251 77
251 79
251 210
251 280
254 31
254 33
254 37
254 38
254 39
254 104
254 256
254 257