简体   繁体   English

将两个复杂的SQL查询合并为一个

[英]Combine Two Complex SQL Queries Into One

My mySQL database has a schools and a users table. 我的mySQL数据库有一个schools和一个users表。 Each school has an id that identifies it. 每所学校都有一个标识它的id When a user joins a school, the user's school column is set to the id of that school. 当用户加入学校时,该用户的“ school列将设置为该学校的id I need to execute a request which does the following: 我需要执行以下操作:

  1. Gets a random school from the database that has users joined to it. 从有用户加入的数据库中获取随机学校。 (Must have at least one student in the database whose school column is set to that school's id .) (数据库中必须至少有一名学生,其“ school列设置为该学校的id 。)
  2. Gets the user at that school with the highest (successes / attempts) value. 获取该学校中具有最高(成功/尝试)价值的用户。
  3. If multiple users at the school are tied for the highest value, the query will select a random user and return it. 如果学校中有多个用户并列最高价值,则查询将选择一个随机用户并将其返回。

All I need in return is the user object. 我所需要的只是用户对象。 I know I could use PHP for some of the work, but I would like to do the majority in the SQL query. 我知道我可以使用PHP进行某些工作,但我想在SQL查询中做大部分工作。 If possible, I would like to do this all in one request. 如果可能的话,我想在一个请求中完成所有这一切。 Is this possible? 这可能吗? So far I've come up with: 到目前为止,我已经提出了:

SELECT  *
FROM  `schools` 
INNER JOIN  `users` ON schools.id = users.school
ORDER BY RAND( ) 
LIMIT 1

at which point I could save the selected school's id in PHP as $schoolID and execute another request: 此时,我可以将选定学校的ID在PHP中保存为$ schoolID并执行另一个请求:

SELECT * 
FROM  `users` 
WHERE school =2
AND attempts !=0
ORDER BY (
successes / attempts
) DESC , RAND( ) 
LIMIT 1

Is there a way to combine them into one SQL query to save server load? 有没有一种方法可以将它们组合为一个SQL查询以节省服务器负载?

Structure: 结构体:

Users Table: 用户表:

+------------+----------+------+-----+---------+----------------+
| Field      | Type     | Null | Key | Default | Extra          |
+------------+----------+------+-----+---------+----------------+
| id         | int(11)  | NO   | PRI |         | auto_increment |
| username   | char(35) | NO   |     |         |                |
| school     | int(11)  | YES  |     |         |                |
| successes  | int(11)  | NO   |     | 0       |                |
| attempts   | int(11)  | NO   |     | 0       |                |
+------------+----------+------+-----+---------+----------------+

Schools Table: 学校表:

+------------+----------+------+-----+---------+----------------+
| Field      | Type     | Null | Key | Default | Extra          |
+------------+----------+------+-----+---------+----------------+
| id         | int(11)  | NO   | PRI |         | auto_increment |
| name       | char(35) | NO   |     |         |                |
+------------+----------+------+-----+---------+----------------+

Sample Data: Users table: 样本数据:用户表:

id  username    school  successes   attempts
1   josh        1       0           0
2   james       2       0           1
3   ashley      2       2           2
4   john        1       1           1
5   will        3       4           8
6   jack        3       1           2

Schools table: 学校表:

id  name
1   school1
2   school2
3   school3 
4   school4

Only schools 1-3 should be able to be chosen from schools , and only user 4 should show if school 1 was chosen, user 3 should show if school 2 was chosen, and either user 5 or 6 should show if school 3 was chosen. 应该只能从schools选择学校1-3,只有用户4应该显示是否选择了学校1,用户3应该显示是否选择了学校2,用户5或6应该显示是否选择了学校3。

Revised again so that it will NOT retrieve all records in the inner query. 再次修改,使其不会在内部查询中检索所有记录。 Safe to use with larger data sets. 可以安全地用于较大的数据集。 The inner select gets a random school which has a valid user record. 内部选择获得具有有效用户记录的随机学校。 The outer select selects on that school record and sorts by success / attempts and limits to one. 外部选择在该学校记录上进行选择,并按成功/尝试和限制进行排序。 Tested with your data. 测试了您的数据。

SELECT s.id, s.name, users.username, users.successes, users.attempts 
FROM ((SELECT schools.id, schools.name from schools INNER JOIN users on schools.id = users.school
    WHERE users.attempts !=0
    ORDER BY RAND()
    LIMIT 1) s)
INNER JOIN users on s.id = users.school
ORDER BY users.successes / users.attempts DESC
LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM