一个复杂的SQL怎么写 - 技术问答

2025-06-22 10:04:07
推荐回答(2个)
回答1:

假设:table_user:用户表(id, usename)table_planet:星球表(userid, people)[php]//子查询方式,不推荐,效率低SELECT name, (SELECT SUM(people) FROM table_planet WHERE userid=table_user.id) AS peopleCount FROM table_user//联合查询方式,推荐SELECT t1.username,SUM(t2.people) AS peopleCount??INNER JOIN table_user AS t1 ON t2.userid=t1.idFROM table_planet AS t2GROUP BY t2.userid[/php]

回答2:

[php]select A.name , SUN(B.p_num) as num from usertable A inner join Startable B on A.u_id = B.u_idgroup by B.u_uid order by num desc;[/php]