如何获得MySQL中的最大列

时间:2016-07-24 10:17:12

标签: mysql sql select max

我希望根据表numberx列中的最大数量获取行的x

例如: 我在 id | number ---------+--------- 9 | 289 10 | 100 24 | 187 54 | 345 表中有以下记录。

id

我希望查询返回SELECT x.id ... 54 number),发现列<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="RTL" lang="Ar"> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <link href="css/style.css" media="screen" rel="stylesheet" type="text/css" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="refresh" content="30"> </head> <body> <form method= "POST" action = "" > <?php include ("connection.php"); ?> <?php $msg = ""; if (isset($_POST['update'])) { if (is_numeric($_POST['question_id'])) { $question_id = mysqli_real_escape_string($link, htmlspecialchars($_POST['question_id'])); $question_text = mysqli_real_escape_string($link, htmlspecialchars($_POST['question_text'])); $test_id = mysqli_real_escape_string($link, htmlspecialchars($_POST['test_id'])); if ($question_text == "" || $test_id == "" ) { $msg = "يرجى تعبيئة كافة الحقول"; } else { $sql = mysqli_query($link, " UPDATE `question` SET `question_text`='".$question_text."',`test_id`= '".$test_id."' WHERE `question_id` = '".$question_id."' ")or die(mysqli_error($link)); /* UPDATE `question` SET `question_text`='".."',`test_id`= '".."' WHERE `question_id` = */ } } else { echo "Error !"; } }else { if (isset($_GET['question_id']) && is_numeric($_GET['question_id']) && $_GET['question_id'] > 0) { $question_id = $_GET['question_id']; $query = mysqli_query($link, "SELECT * FROM `question` WHERE `question_id` = '".$question_id."' ") or die(mysqli_error($link)); $isFirst = TRUE; while ($row = mysqli_fetch_assoc($query)) { $question_id = $row ['question_id']; $question_text = $row ['question_text']; $test_id = $row['test_id']; if ($isFirst) { ?> <p> <label>رقم الاختبار</label> <td><input type="text" name="test_id" value="<?php echo $test_id ; ?>" /></td> </p> <?php $isFirst = FALSE; } ?> <p> <label><p>الحقل المخصص للسؤال</p></label> <textarea name = "question_text"><?php echo $question_text ; ?> </textarea> </p> <?php $query2 = mysqli_query($link,"SELECT * FROM `answers` WHERE answers.question_id = '".$question_id."' ") or die(mysqli_error($link)); while($row2 = mysqli_fetch_assoc($query2)){ $answer_text = $row2 ['answer_text']; $correct = $row2 ['correct']; /* SELECT question.question_id,question.question_text,question.test_id,test.test_name FROM question,test WHERE question.test_id = test.test_id AND question.question_id= '".$question_id."' */ /* SELECT `answer_text`, `correct` FROM `answers` WHERE answers.question_id = 6 AND answers.correct = 1 */ ?> <p> <label>الخيار الاول</label> <input type="text" name="" value="<?php echo $answer_text ; ?>" /> </p> <?php } ?> <p> <input type="submit" name="update" value="تحديث البيانات" class="button save" /> </p> <p> <input type="hidden" name = "question_id" value="<?php echo $_GET['question_id']; ?>" /> </p> <?php } } } ?> </form> </body> </html> 最大值为345 < /强>

我怎样才能成功呢?

2 个答案:

答案 0 :(得分:1)

请尝试以下查询:

解决方案#1:

SELECT 
x.*
FROM x
INNER JOIN 
(
  SELECT 
   MAX(number) AS max_number
  FROM x
) AS maxTable
ON x.number = maxTable.max_number

<强>解释

SELECT 
  MAX(number) AS max_number
FROM x;

结果:

<强> maxTable:

max_number
   345

现在在inner joinx上的maxTable表与maxTable.max_number上面的x.number之间建立一个 ===================== | id | number | ===================== | 9 | 289 | --------------------- | 10 | 100 | --------------------- | 24 | 187 | --------------------- | 54 | 345 | ---------------------

x table:

maxTable.max_number

因此,在最终输出中,您只会获得number列中WHERE IN的条目。

解决方案#2:

您也可以使用SELECT * FROM x WHERE x.number IN ( SELECT MAX(number) FROM x ) 来完成此任务。

WrapWholeWords

答案 1 :(得分:0)

为什么你不这样做:

select t.id
from t
order by number desc
limit 1;