mysql - SQL One to Many column association rows -
given table below want select rows same 'code' associated multiple 'sub_code'.
+------+------------+-------------+ | div | code | sub_code | +------+------------+-------------+ | 11 | 1000 | 1212 | | 11 | 1000 | 1213 | | 11 | 1000 | 3434 | | 11 | 1000 | 1000 | | 11 | 1000 | 3000 | | 11 | 3000 | 1213 | | 11 | 2000 | 1212 | | 20 | 1500 | 5656 | | 20 | 1500 | 1213 | +------+------------+-------------+
for above table result should be
+------+------------+-------------+ | div | code | sub_code | +------+------------+-------------+ | 11 | 1000 | 1212 | | 11 | 1000 | 1213 | | 11 | 1000 | 3434 | | 11 | 1000 | 1000 | | 11 | 1000 | 3000 | | 11 | 1500 | 5656 | | 11 | 1500 | 1213 | +------+------------+-------------+
this tried, how ever results fails.
select code table_name (count(sub_code) > 1);
first fetch code
s multiple sub_code
s exists; project table columns filtering using above results nested query:
select * table_name code in (select code table_name group code having count(sub_code) > 1);
Comments
Post a Comment