mysql - Add additional joins to already joined table in SQL -
mysql - Add additional joins to already joined table in SQL -
i have existing query using joins (thanks radar) data.
working sqlselect ifnull(f.field_full_name_value, 'no value'), u.name, u.uid, n.title, n.nid, a.timestamp, d.field_video_duration_value duration db_node_view_count bring together db_node n on a.nid = n.nid bring together db_field_data_field_video_duration d on n.nid = d.entity_id bring together db_users u on a.uid = u.uid , u.uid <> 1 left bring together db_field_data_field_full_name f on u.uid = f.entity_id order u.uid desc
what want want extend query show roles of db_users u
. there table called db_roles
, contain role names primary key rid. sec table db_users_roles
, contains matching uid
(from db_users u) , rid show user selected role.
so did under on a.uid = u.uid
, added join db_users_roles ur on u.uid = ur.uid bring together db_role r on ur.rid = r.rid
. works fine shows duplicate rows. thought why it's happening?
select ifnull(f.field_full_name_value, 'no value'), r.name, u.name, u.uid, n.title, n.nid, a.timestamp, d.field_video_duration_value duration db_node_view_count bring together db_node n on a.nid = n.nid bring together db_field_data_field_video_duration d on n.nid = d.entity_id bring together db_users u on a.uid = u.uid left bring together db_users_roles ur on u.uid = ur.uid left bring together db_role r on ur.rid = r.rid , u.uid <> 1 left bring together db_field_data_field_full_name f on u.uid = f.entity_id order u.uid desc
update with help joe, here slight update:
select ifnull(f.field_full_name_value, 'no value'), group_concat(r.name), u.name, u.uid, n.title, n.nid, a.timestamp, d.field_video_duration_value duration db_node_view_count bring together db_node n on a.nid = n.nid bring together db_field_data_field_video_duration d on n.nid = d.entity_id bring together db_users u on a.uid = u.uid left bring together db_users_roles ur on u.uid = ur.uid left bring together db_role r on ur.rid = r.rid , u.uid <> 1 left bring together db_field_data_field_full_name f on u.uid = f.entity_id grouping f.field_full_name_value order u.uid desc
some users in (drupal) database may have more 1 role. original query had (simplified bit) 1 row per node, modified query duplicate rows each role of user.
you might want modify query include grouping n.nid, u.uid , alter select field list include group_concat(r.name) rather r.name.
mysql join
Comments
Post a Comment