简体   繁体   English

从单独的列为MIN()的列中选择一个SQL值

[英]Select an SQL value from column where separate column is MIN()

I am developing an assignment function for orders and I am trying to assign the order to a user who has the least pending requests. 我正在开发订单分配功能,并试图将订单分配给请求最少的用户。 I want to get the user id where the value for pending is the least of all of the users. 我想获取用户ID,其中待处理的值是所有用户中最少的。

Example: 例:

userID    pending
------    -------
0         3
1         0 <----This user has the least pending of all
2         4

How do I set up an SQL Query to return the user ID with the least amount of pending request of the table? 如何设置SQL查询以返回表中未完成请求最少的用户ID?

$sql="SELECT user_id FROM writerdata_tb WHERE pending = '//Minimum pending here//' LIMIT 1";
$assign = $conn->query($sql);
$row = $assign->fetch_assoc();

A comparison of various possible answers proposed: 建议比较各种可能的答案:

EXPLAIN SELECT aa.userId, aa.pending
FROM writerdata_tb AS aa
INNER JOIN writerdata_tb AS bb 
ON aa.userId = bb.userId
WHERE aa.pending >= bb.pending
ORDER BY aa.pending ASC
LIMIT 1;

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra   
1   SIMPLE  aa  ALL     PRIMARY     NULL    NULL    NULL    4   Using temporary; Using filesort
1   SIMPLE  bb  ALL     PRIMARY     NULL    NULL    NULL    4   Using where; Using join buffer (Block Nested Loop)

EXPLAIN SELECT userId, pending
FROM writerdata_tb
WHERE pending = (
    SELECT MIN(pending)
    FROM writerdata_tb 
);

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra   
1   PRIMARY     writerdata_tb   ALL     NULL    NULL    NULL    NULL    4   Using where
2   SUBQUERY    writerdata_tb   ALL     NULL    NULL    NULL    NULL    4   NULL

EXPLAIN SELECT userId, pending
FROM writerdata_tb
ORDER BY pending ASC
LIMIT 1;

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra   
1   SIMPLE  writerdata_tb   ALL     NULL    NULL    NULL    NULL    4   Using filesort

EXPLAIN SELECT t.userId
FROM writerdata_tb AS t
GROUP BY (
    t.userId
)
ORDER BY SUM(pending) ASC
LIMIT 1;

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra   
1   SIMPLE  t   index   PRIMARY     PRIMARY     4   NULL    4   Using temporary; Using filesort

You can order results by column pending (see comment by Simone Nigro for the query). 您可以按列待定顺序对结果进行排序(有关查询,请参见Simone Nigro的评论)。 Using the limit word, in the result you will get only the first row, in this case you will get the result with minium pending value. 使用限制字,在结果中您将仅获得第一行,在这种情况下,您将获得具有最小未决值的结果。

I see now the case of Aleksandar Miladinovic, if in the table there are many rows with same value of pending : you can execute: 我现在看到Aleksandar Miladinovic的情况,如果表中有很多行具有相同的未决值:您可以执行:

  1. A query to get the mininium value of pending column 查询以获取未决列的最小值
  2. Use the value obtained in the previous step to receive the rows with that value 使用上一步中获得的值来接收具有该值的行

    SELECT user_id FROM writerdata_tb WHERE pending=(SELECT min(pending) FROM writerdata_tb) SELECT user_id FROM writerdata_tb WHERE未决=(SELECT min(pending)FROM writerdata_tb)

尝试这种方式:从writerdata_tb中选择userID,其中未决=(从writerdata_tb中选择min(pending));

If UserID's are repeated 如果用户ID重复

SELECT
    t.userID
from 
    tasks as t
group by (t.userID)
order by sum(pending) asc 
limit 1

else 其他

SELECT
    t.userID
from 
    tasks as t
order by pending asc 
limit 1

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

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