MySQL multi-table connection query considerations

1. Multi-table join query deduplication problem: you can add a group by id; such as:

<!--Get paging data-->
	
		select * from user u left join userrole ur on u.id=ur.uid left join role r on r.rid=ur.rid
		where 1=1	
		group by u.id
		limit 0,7
	</select>

2. Multi-table join query to find the total count:

select count(*) from
		(select u.id from user u left join userrole ur on u.id=ur.uid left join role r on r.rid=ur.rid
		group by u.id) as a

In the same way, group by u.id is de-duplicated here, and as a is an alias for this internal table query. Although it is useless, it will report an error if it is removed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325456450&siteId=291194637